3
votes

I'd like to use JSR-303 annotations for parameter validation of methods of a class other than a JAX-RS resources. Using the JEE containers it works on EJB/CDI beans, but I cannot get it running in Spring. The code might look like below.

Which maven dependency or other configuration am I missing? (I don't want to call the Validator myself. I want spring does the work for me here.)

Note that the Person class is not a JPA entity, hence Hibernate Validator as the implementation of JPA cannot do the work here.

public class Person{

    @NotNull
    private String firstName;

}

@Service
public class PersonServiceImpl{

    public void create(@Valid Person person){
        ....
    }
}
1
what error you getting??? - OPK
@JasonZ Here is the point when the firstName is null I don't get any error! I except to get a javax.validation.ConstraintViolationException which is not thrown. - LAC

1 Answers

0
votes

According to official documentation of Spring you can inject validator to your service, look link http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html.

Example:

import javax.validation.Validator;

@Service
public class MyService {

@Autowired
private Validator validator;