Skip to content
Advertisement

Validate request with List as a field with javax.validation

I am using javax.validation for validating request in an API developed using spring.

I have to validate List<Integer> so that all values must be a positive number and size of the list must be exactly equal to some fix number e.g. 2.

JavaScript

Valid request should be [ 1,2]

Invalid request should be [-1,2] or [1,2,3]

Advertisement

Answer

EDIT:

Just realized how you want to get your payload. In that case I think best would be to write your own constraint validator (annotation):

Annotation:

JavaScript

Validator:

JavaScript

RestController:

JavaScript

enter image description here

enter image description here

enter image description here

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement