0

I have the following API setup:

CONTROLLER:

@PostMapping(path = "/post", consumes = "application/json", produces = "application/json")
public ResponseEntity<Object> receiveData(@Valid @RequestBody ArrayList<Person> persondata) {       
    }

BEAN:

@Component
public class Person {

@JsonProperty(value="name",required = true)
@Size(min=1)
private String name;

public Person(@JsonProperty(value="name", required = true) @Size(min=1) String name) {
    this.name = name;
}

When I send the following requestbody through postman, it does not trigger the @Size constraint as expected:

[
{"name":"Pamela"},
{"name":"John"},
{"name":""}
]
sotn
  • 1,833
  • 5
  • 35
  • 65
  • Is this an accurate representation of the JSON you're sending through? You're sending through objects with a 'name' property, but your code has relabelled the 'name' property on Person as 'id' for JSON purposes. – Polly Shaw Mar 12 '19 at 14:49
  • This may be what you're after: https://stackoverflow.com/questions/28150405/validation-of-a-list-of-objects-in-spring – Polly Shaw Mar 12 '19 at 15:15
  • @PollyShaw, thats a typo. Fixed. – sotn Mar 12 '19 at 15:43
  • @PollyShaw, thanks for pointing me the solution. Worked perfectly. – sotn Mar 12 '19 at 15:52
  • Possible duplicate of [Validation of a list of objects in Spring](https://stackoverflow.com/questions/28150405/validation-of-a-list-of-objects-in-spring) – Polly Shaw Mar 12 '19 at 15:57

0 Answers0