繁体   English   中英

在服务类中启用JSR-303批注验证(不在Spring控制器或JAX-RS资源中)

[英]Enabling JSR-303 annotation validation in a service class (not in Spring controllers nor in JAX-RS resources)

我想使用JSR-303注释来进行除JAX-RS资源之外的类的方法的参数验证。 使用JEE容器可以在EJB / CDI bean上运行,但我无法在Spring中运行它。 代码可能如下所示。

我错过了哪种maven依赖或其他配置? (我不想调用Validator自己。我想春天确实对我的工作在这里。)

注意,Person类不是JPA实体,因此Hibernate Validator作为JPA的实现不能在这里工作。

public class Person{

    @NotNull
    private String firstName;

}

@Service
public class PersonServiceImpl{

    public void create(@Valid Person person){
        ....
    }
}

根据Spring的官方文档,您可以为您的服务注入验证器,查看链接http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html

例:

import javax.validation.Validator;

@Service
public class MyService {

@Autowired
private Validator validator;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM