0

I am working on the JSR validations using spring (4.2.0.RELEASE), hibernate validator (5.2.1.Final) and validation api (1.1.0.Final) for the backend appliation with the below configurations,

<bean id="validatorFactory" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="validationMessageSource" ref="messageSource" />
</bean>

<bean class="org.springframework.expression.spel.standard.SpelExpressionParser" />

but None of the JSR 303 annotations are working in my application.

Note: Added jsr 303 annotations on POJO class and @Validated on the service class (Which is using the POJO) also tried adding @Validated on the method level.

Update: Service Interface

@Validated 
public interface SampleService {

@NotNull
@Valid
Account getAccount( @NotNull @Valid String customerKey, @NotNull String name);

Service Implementation

@Service
@Validated
@Transactional( readOnly=true )
public class SampleServiceImpl
    implements SampleService
{
    private final SampleDao sampleDao;

@Inject
public SampleServiceImpl( SampleDao sampleDao)
{
    this.sampleDao= sampleDao;
}


@Override
@Validated
public Customer getAccount( String customerKey, String name)
{
    try {
        return sampleDao.getAccount( customerKey, name);
    }
    catch ( EmptyResultDataAccessException e ) {
        throw new NotFoundException( e );
    }
}
Sunil Rk
  • 999
  • 6
  • 12
  • 35

2 Answers2

0

There are a few things:

1- You need to validate POJOs, so create a POJO and annotated it with the validations you want, i.e.:

public class Customer {

    @NotNull
    private String name;

    @NotNull
    private String customerKey;

    ...

}

2- The @Valid annotation needs to be on the implementation:

@Override
public Customer getAccount(@Valid Customer customer, BindingResult bindingResult) {}

3- Make sure you have <mvc:annotation-driven/> on your Spring xml.

BindingResult is not required on your methods, but it gives you the validations errors found and allow you to do something with them before sending the response back.

dambros
  • 4,252
  • 1
  • 23
  • 39
  • Hi, Its not working :( and i am not using 'mvc'. It is a back end application no UI. – Sunil Rk Apr 13 '16 at 09:06
  • It doesn't matter if UI or not. I have a restapi which I use JSR303 extensively, exactly like I posted above. It is hard to tell which config you are missing, without seeing the entire project. If it is just a mock project, post it to github and I can see what is missing. Did you add the config on item number 3? – dambros Apr 13 '16 at 09:09
  • Are you using the same versions of jars ? – Sunil Rk Apr 13 '16 at 09:17
  • Spring 4.2 and HibernateValidator 5.2.4, but it doesn't matter. The problem here is config. – dambros Apr 13 '16 at 09:19
0

Duplicate question, please read on this one

Validation on service layer

This link explains that by default spring annotation validation is supported only in controller layer. For service layer, either you just reuse some spring components and tweak some configuration or roll your own AOP. But I suggest just choose the former. REUSE.

Community
  • 1
  • 1
vine
  • 846
  • 6
  • 10