[英]Is it possible to use @Valid in Service layer? What could be the workaround?
我正在编写一个验证服务,我不需要直接验证传入的请求对象,而是在进行一些操作后对其进行验证。
@Service
public class FooCreationService {
private String validateFoo(@Valid Foo foo) {
return "";
}
}
public class Foo {
@Size(min=1, max=60)
@NotBlank(message="Product description required.")
private String fooDescription;
}
当 Spring Boot 发现使用 @Valid 注释的参数时,它会自动引导默认的 JSR 380 实现——Hibernate Validator——并验证该参数。 当目标参数验证失败时,Spring Boot 会抛出 MethodArgumentNotValidException 异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.