Dar*_*til 48 java spring annotations constraints bean-validation
什么样的配置,需要使用注释来自javax.validation.constraints像@Size,@NotNull等等?这是我的代码:
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class Person {
@NotNull
private String id;
@Size(max = 3)
private String name;
private int age;
public Person(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在另一个类中使用它时,验证不起作用(即创建对象时没有错误):
Person P = new Person(null, "Richard3", 8229));
Run Code Online (Sandbox Code Playgroud)
为什么这不适用于id和name?我还需要做什么?
atr*_*ain 50
要使JSR-303 bean验证在Spring中工作,您需要以下几点:
<mvc:annotation-driven />validation-api-1.0.0.GA.jar看起来你已经有了)hibernate-validator-4.1.0.Final.jar@Valid,然后BindingResult在方法签名中包含a 以捕获错误. 例:
@RequestMapping("handler.do")
public String myHandler(@Valid @ModelAttribute("form") SomeFormBean myForm, BindingResult result, Model model) {
if(result.hasErrors()) {
...your error handling...
} else {
...your non-error handling....
}
}
Run Code Online (Sandbox Code Playgroud)
Art*_*tem 28
您应该使用Validator来检查您的类是否有效.
Person person = ....;
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
Set<ConstraintViolation<Person>> violations = validator.validate(person);
Run Code Online (Sandbox Code Playgroud)
然后,迭代违规设置,您可以找到违规.
xCo*_*lus 12
几年后我来到这里,感谢上面atrain的评论,我可以修复它。就我而言,我缺少接收用. 它解决了这个问题。@Valid@Size
我不需要添加任何额外的注释,例如@Valid或@NotBlank到用 注释的变量@Size,只需变量中的约束以及我在 API 中提到的内容...
波乔类:
...
@Size(min = MIN_LENGTH, max = MAX_LENGTH);
private String exampleVar;
...
Run Code Online (Sandbox Code Playgroud)
API类:
...
public void exampleApiCall(@RequestBody @Valid PojoObject pojoObject){
...
}
Run Code Online (Sandbox Code Playgroud)
谢谢和欢呼
就我而言,我使用的是 Spring Boot 2.3.0 版。当我将我的 Maven 依赖项更改为使用 2.1.3 时,它起作用了。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
就我而言,原因是hibernate-validator 版本。可能新版本不再支持某些内容。
我变了:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我将版本从7.0.1.Final降级到6.0.2.Final,这对我有帮助。
你将不得不调用一个验证实体,如果要验证它.然后你将得到一组ConstraintViolationException,它基本上显示你的Entity的哪个字段存在约束违规以及究竟是什么.也许您也可以共享一些您期望验证实体的代码.
如果在事务期间使用多个数据修改或在获得验证异常时执行其他操作,则常用的技术是在@PrePersist和回滚事务中进行验证.
你的代码应该是这样的:
@PrePersist
public void prePersist(SomeEntity someEntity){
Validator validator = Validation.buildDefaultValidatorFactory.getValidator();
Set<ConstraintViolation<SomeEntity>> = validator.validate(someEntity);
//do stuff with them, like notify client what was the wrong field, log them, or, if empty, be happy
}
Run Code Online (Sandbox Code Playgroud)
您也可以简单地@NonNull与lombok 库一起使用,至少对于@NotNull场景. 更多详情:https : //projectlombok.org/api/lombok/NonNull.html