我正在验证 spring-boot 2.3.1.RELEASE Web 应用程序中的 REST 服务请求/bean。目前,我正在使用 Hibernate Validator,但我愿意使用任何其他方式进行验证。
说,我有一个模型Foo,我在Rest Controller. 我想验证如果completionDate不是null那么status应该“完成”或“关闭”。
@StatusValidate
public class Foo {
private String status;
private LocalDate completionDate;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个自定义类级别的注释@StatusValidate。
@Constraint(validatedBy = StatusValidator.class)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface StatusValidate {
String message() default "default status error";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Run Code Online (Sandbox Code Playgroud)
我创建了StatusValidator类。
public class StatusValidator implements ConstraintValidator<StatusValidate, Foo> {
@Override
public void …Run Code Online (Sandbox Code Playgroud) 我有一个 JSON:
{
"stringField" : 1234,
"booleanField": true,
"numberField": 1200.00
}
Run Code Online (Sandbox Code Playgroud)
我使用对象映射器将 json 反序列化为:-
@Data
class SomeClass {
String stringField;
boolean booleanField;
float numberField;
}
Run Code Online (Sandbox Code Playgroud)
我希望 objectMapper 抛出错误,因为根据 json 规范,字符串字段的值必须双引号。我怎样才能让 objectMapper 抛出错误?
我这里有一个例子:
import java.lang.ref.WeakReference;
public class WeakRefTest {
public static void main(String[] args) {
Object obj = new Object();
WeakReference<Object> weakRef = new WeakReference<Object>(obj);
int i = 0;
while (true) {
if (weakRef.get() != null) {
i++;
System.out.println("The object is alive for " + i + " loops - " + weakRef);
} else {
System.out.println("The object has been collected.");
break;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
运行这个程序时,我们会"The object has been collected."在一段时间后得到输出,这意味着对象将被 gc-ed。
但是,仍然存在一个名为"obj"链接到对象的强引用,如何回收?因为后来JVM发现没有强引用使用,所以不是强可达?
我尝试了多种将 json 文件存储在数据库中的方法,但最终为每个条目创建了不同的列。
我想将它存储为单个列中的“json”类型。是否可以?
我的json文件。
用户.json
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
...
]
Run Code Online (Sandbox Code Playgroud)
这是一个 spring-boot 应用程序,我有相关的控制器和服务。
在我的域包中。(地址和公司是可嵌入的类)
用户.java
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": …Run Code Online (Sandbox Code Playgroud) 我们有一个基于 Spring-Boot (2.3.10.RELEASE) 的应用程序,它使用RestTemplate.
如果任何 REST API 返回任何 4xx 或 5xx HTTP 错误代码以及消息正文,则不会记录完整的消息正文。
这是一个最小的可重现示例:
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
@Slf4j
public class RestTemplateTest {
@Test
void shouldPrintErrorForRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
try {
restTemplate.getForEntity("http://hellosmilep.free.beeceptor.com/error/notfound", String.class);
} catch (Exception e) {
log.error("Error calling REST API", e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
10:28:11.347 [main] ERROR com.smilep.java.webapp.RestTemplateTest - Error calling REST API
org.springframework.web.client.HttpClientErrorException$NotFound: 404 Not Found: [{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": …Run Code Online (Sandbox Code Playgroud) 我正在为休息控制器编写junit。我只想加载与控制器相关的最小上下文,我认为这就是@WebMvcTest加载上下文的方式。但是 junit 正在加载完整的 Spring 上下文,并且由于无法创建某些 bean 而失败。
我已经搜索并阅读了许多关于 stackoverflow 的问题,但没有一个解决方案可以排除特定的配置。为控制器编写junit时如何加载最小上下文?或者有什么办法可以排除一些配置类?我使用的是 Spring-boot 2.2.2.RELEASE、Java 8 和 Junit 4。
Junit(我试图排除加载一些 bean 和配置,但它不起作用):
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = OrderController.class, excludeFilters = {@Filter(classes = Configuration.class), @Filter(type = FilterType.REGEX, pattern = "com\\.foo\\..*")})
@TestPropertySource(properties = { "spring.cloud.config.server.bootstrap:false" })
public class OrderControllerTest {
@MockBean
private OrderService orderService;
@Autowired
private MockMvc mockMvc;
@Test
public void test() throws Exception {
mockMvc.perform(get("/internal/order/123"));
// Further code to verify the response
}
}
Run Code Online (Sandbox Code Playgroud)
控制器
@Slf4j
@Validated
@RestController
@RequestMapping("/internal")
public class OrderController {
@Autowired …Run Code Online (Sandbox Code Playgroud) 我正在做一门课程的作业,我需要全面了解这种方法

这些是属性和构造函数,它是咖啡机的程序,这是 Recipe 类
public class Recipe {
private String name;
private int price;
private int amtCoffee;
private int amtMilk;
private int amtSugar;
private int amtChocolate;
/**
* Creates a default recipe for the coffee maker.
*/
public Recipe() {
this.name = "";
this.price = 0;
this.amtCoffee = 0;
this.amtMilk = 0;
this.amtSugar = 0;
this.amtChocolate = 0;
}
Run Code Online (Sandbox Code Playgroud)
我用过
/*
* setPrice test
*/
@Test
public void testSetPrice_1() throws RecipeException {
r1.setPrice("25");
r1.setPrice("0");
}
/*
* setPrice test
*/ …Run Code Online (Sandbox Code Playgroud) 如何从 java springboot 应用程序中的配置 yml 文件加载对象列表?
我已经尝试了几个来源:
spring-boot-configurationproperties-example
堆栈:Java 11、SpringBoot 2.1.4、Lombok、.yml 格式的配置文件。
我尝试实现简单的@Component,它将从配置文件加载数据。
配置值为:
allowed:
- first-crossroad: ONE
second-crossroad: TWO
third-crossroad: TWO
fourth-crossroad: THREE
- first-crossroad: ONE
second-crossroad: THREE
third-crossroad: TWO
fourth-crossroad: ONE
Run Code Online (Sandbox Code Playgroud)
用于数据加载的 Java 类是:
allowed:
- first-crossroad: ONE
second-crossroad: TWO
third-crossroad: TWO
fourth-crossroad: THREE
- first-crossroad: ONE
second-crossroad: THREE
third-crossroad: TWO
fourth-crossroad: ONE
Run Code Online (Sandbox Code Playgroud)
Java CrossroadCombination 对象定义:
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "")
@Data
public class AllowedCrossroads {
private List<CrossroadCombination> allowed;
}
Run Code Online (Sandbox Code Playgroud)
我希望在应用程序运行期间加载值。但我收到错误:
Property: allowed[0].first-crossroad
Value: …Run Code Online (Sandbox Code Playgroud) public static void main (String[]args) {
ArrayList<Integer> x= new ArrayList<Integer>();
for (int i=0;i<Integer.MAX_VALUE;i++) {
x.add(i);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我的内存不足?有什么办法可以将很多值存储到列表中,例如从Integer.Min_value到Integer.Max_value?
java ×9
spring-boot ×5
arraylist ×1
hibernate ×1
jackson ×1
jacoco ×1
json ×1
junit ×1
junit4 ×1
list ×1
mysql ×1
objectmapper ×1
reference ×1
resttemplate ×1
spring ×1