Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javax @NotNull annotation usage

I have a simple method to get a list of documents for a given companyId. Here is the method:

@Override
public List<Documents> getDocumentList(@NotNull Integer companyId) {
    Company company = new Company(companyId);
    return this.documentRepository.findByCompany(company);
}

I wanted to use Javax validation constraints to ensure that the companyId being passed in, is not null. But it seems to not have any effect, as I'm able to pass in a null value, and it flows down to the findByCompany call on the repository. I also added @Valid before @NotNull to force validation, but that too didn't do anything.

I could always write a couple of lines to check for a null value, but wanted to use javax.validation annotations to make the code more readable and concise. Is there a way to make the annotations work on method params?

like image 232
sisanared Avatar asked Apr 28 '17 02:04

sisanared


1 Answers

To activate parameter validation, simply annotate the class with @Validated

import org.springframework.validation.annotation.Validated;
like image 79
sisanared Avatar answered Oct 21 '22 13:10

sisanared