Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Validation not working with Generics

I try to validate buy Spring Boot Validation but I can not valid Generics Type. I implementation 'org.springframework.boot:spring-boot-starter-validation' in build.gradle I have a object:

public class BaseAPIRequestDTO<T, U> {
@NotNull
private T content;
@NotNull
private U info;
@NotEmpty
private String signature;}

and Generic Object:

public class ContentRequestDTO {
@NotNull
@NotEmpty(message = "Thiếu transactionId")
private String transactionId;
private DataRequestDTO data;}

..... and controller:

   public ResponseEntity<BaseAPIResponseDTO<ContentResponseDTO, InfoResponseDTO>> connnnnn(@RequestBody @Valid BaseAPIRequestDTO< @Valid ContentRequestDTO, @Valid  InfoRequestDTO> request, BindingResult bindingResult, HttpServletRequest httpServletRequest)

But I when I clear transactionId on of ContentResponseDTO in request, bindingResult has 0 error result

What do I need to do now? Thank so much.

like image 311
Hoang Truong Avatar asked Aug 06 '26 04:08

Hoang Truong


1 Answers

Did you try to add @Valid in BaseAPIRequestDTO attributes?

Example:

public class BaseAPIRequestDTO<T, U> {
   @Valid
   @NotNull
   private T content;
   @Valid
   @NotNull
   private U info;
   @NotEmpty
   private String signature;
}

This should work.

like image 143
GabrieRabelo Avatar answered Aug 07 '26 19:08

GabrieRabelo