[英]Spring Rest Add @NotEmpty in @RequestBody without creating request POJO
I want to add @NotEmpty
in @RequestBody
, but i don't want to create additional POJO just for the request, how can i do that?我想在
@RequestBody
添加@NotEmpty
,但我不想仅为请求创建额外的 POJO,我该怎么做?
I want to do like the following, but it still return me 201 Created
status code when i put []
in request body, it means that @NotEmpty
is actually not working.我想执行以下操作,但是当我将
[]
放入请求正文时,它仍然返回201 Created
状态代码,这意味着@NotEmpty
实际上不起作用。
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public void create(@Valid @NotEmpty @RequestBody Set<String> request) {
.....
}
I DO NOT WANT to do something like this, but the @NotEmpty
works in this case :我不想做这样的事情,但
@NotEmpty
在这种情况下工作:
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public void create(@Valid @NotEmpty @RequestBody SampleRequest request) {
.....
}
SampleRequest class SampleRequest 类
public class SampleRequest {
@NotEmpty
private Set<String> name;
..Setter and Getter
}
您还需要使用@Validated ( https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/validation/annotation/Validated.html ) 注释您的控制器、方法或参数,以便触发验证
Add annotation @Validated
on controller class and then validation will be work for method parameters. 在控制器类上添加注释
@Validated
,然后对方法参数进行验证即可。 For details, you can look at tutorial here https://www.baeldung.com/spring-validate-list-controller . 有关详细信息,您可以在此处查看教程https://www.baeldung.com/spring-validate-list-controller 。
First thing I would do is check that there's a validation implementation on your classpath.我要做的第一件事是检查您的类路径上是否有验证实现。 The @NotEmpty annotation comes from a separate jar (
validation-api.jar
), so it's possible that, while your code compiles, validation is not being enforced at runtime. @NotEmpty 注释来自一个单独的 jar (
validation-api.jar
),因此有可能在您的代码编译时未在运行时强制执行验证。 You should see a dependency named something like hibernate-validator.jar
您应该会看到一个名为
hibernate-validator.jar
类的依赖hibernate-validator.jar
Failing that, you could try replacing it with @Size(min=1)
如果失败,您可以尝试将其替换为
@Size(min=1)
As you're not validating complex Java objects like DTOs, so you can put constraint (validation annotations) on your method parameters.由于您没有验证像 DTO 这样的复杂 Java 对象,因此您可以在方法参数上放置约束(验证注释)。
You only need to do two things你只需要做两件事
1-put constraint (valiadtion) annotation before the method parameter
方法参数前的 1-put 约束(验证)注解
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public void create(@RequestBody @NotEmpty SampleRequest request) {
.....
}
2-Put @Validated on the class
2-在类上放置@Validated
@Validated
public class testController{
//..
}
Finally, we have
最后,我们有
@Validated
public class testController{
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public void create(@RequestBody @NotEmpty SampleRequest request) {
//..
}
}
Some helpful link一些有用的链接
Spring REST Validation Example Spring REST 验证示例
All You Need To Know About Bean Validation With Spring Boot 关于使用 Spring Boot 进行 Bean 验证你需要知道的一切
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.