2

I have a Spring Boot app where I need to perform validation of an object.

Object which has to be validated:

public class MyObject {
   @NotNull
   @Size(min=5)
   @ApiModelProperty(value = "first name", required=true)
   private String firstName;

   //getters and setters for firstName
}

Now in my controller I have two methods-

public ResponseEntity<RespObject> getSingleResp(@Valid @RequestBody MyObject myObj) {
//Bean validation works I get a 400 if validation fails, only if it is valid then execution enters this method
}

public ResponseEntity<RespObject[]> getMultiResp(@Valid @RequestBody MyObject[] myObjs) {
//Bean validation does not happen here and starts execution of this method
}

I have also tried adding @NotNull @NotEmpty in between @Valid @RequestBody and MyObject[] myObjs to see if that validation happens, but it doesn't seem to work.

Am I missing something silly here or does Bean validation doesn't work for the individual elements of an array? If it does not work then is there a work-around to solve this?

I found a similar question for List, but the solution over there doesn't seem to work for me. Validation of a list of objects in Spring

WhiteBird
  • 97
  • 8

0 Answers0