Reputation: 939
I have a Bean,
@Data
@NoArgsConstructor
public final class PersonRequest {
@NotNull
@JsonProperty("nameList")
private List<Person> nameList;
}
and Person POJO,
@Data
public class Sensor {
@NotNull
@JsonProperty("id")
private int id;
@NotNull
@JsonProperty("name")
@Min(1)
private String name;
}
I am sending JSON request and added @Valid
in my controller. I am sending request as below,
{
"nameList": [
{
"id": 1,
"name": "John"
},
{
"id": 2,
"name": "Alex"
}
]
}
When i send request without id
and name
not validating. I tried using @Valid private List<Person> nameList;
also but no luck. I use Spring boot 2.3.2.
UPDATED:
when i add one more attribute, this also say bad request when i pass date in request.
@NotNull
@JsonProperty("startTime")
@DateTimeFormat(pattern = "yyyy-MM-dd'T'hh:mm:ss", iso =
DateTimeFormat.ISO.DATE_TIME)
@Valid
private LocalDateTime startTime;
Upvotes: 0
Views: 359
Reputation: 1808
The @Valid
annotation in your controller triggers the validation of the PersonRequest
object, passed as request body. To validate also the Person
objects contained in PersonRequest
, you need to annotate that field with @Valid
too.
@Data
@NoArgsConstructor
public final class PersonRequest {
@NotNull
@JsonProperty("nameList")
@Valid
private List<Person> nameList;
}
Upvotes: 1