3

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

2
  • Did you try replacing MyObject[] with List<MyObject> Commented Mar 9, 2021 at 14:49
  • can you paste your entire controller class? or check if you have @Validated annotation on the class level in your controller Commented Mar 10, 2021 at 7:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.