I'm having trouble getting the @Valid annotation to fire in my Spring Controller.
Basically I have the following class:
import lombok.Getter;
import lombok.Setter;
import javax.validation.constraints.NotBlank;
@Getter
@Setter
public class PlayListDTO {
@NotBlank(message = "playListName is required")
private String playListName;
private String playListDescription;
private String playListUrl;
private String playListImageUrl;
}
And the following controller:
@PostMapping(path = "")
public PlayListUGC addPlayList(@RequestBody HashSet<@Valid PlayListDTO> playListDTO,
Authentication authentication,
@RequestHeader(name = "Authorization") String token) {
logger.info("Received Request to create new PlayList from User {}", authentication.getName());
return playListService.savePlayList(playListDTO, authentication.getName());
}
However if Post the following JSON the validation is not getting triggered:
[
{
"playListName":"",
"playListDescription":"200",
"playListUrl":"1234",
"playListImageUrl": "playListImageUrl1"
},
{
"playListName":"asdadsad",
"playListDescription":"500",
"playListUrl":"1235",
"playListImageUrl": "playListImageUrl"
}
]
Now, if I change my controller to take a RequestBody of just PlayListDTO the validation works i.e.
@Operation(summary = "Allows a logged in user to add a playlist")
@PostMapping(path = "")
public PlayListUGC addPlayList(@RequestBody @Valid PlayListDTO playListDTO,
Authentication authentication,
@RequestHeader(name = "Authorization") String token) {
logger.info("Received Request to create new PlayList from User {}", authentication.getName());
return playListService.savePlayList(playListDTO, authentication.getName());
}
Can anyone explain to me why the validation does not work when its on a HashSet collection?
Thanks