title: Not able to validate request body in Spring boot with @Valid
body: <p>I want to validate my request body with <code>@Valid</code> annotation, but it's not working in Spring Boot</p>
  
  <p>I have a Request class within JAR file which I can't modify with two fields. One field is of type Object. My controller class accept this class object as a request body. When I pass my below JSON to the controller, validation is not working. Below are code samples.</p>
  
  <p>Request Class:</p>
  
  <pre><code>public class Request {
  
      Object data;
      Map&lt;String, Object&gt; meta;
  
      public &lt;T&gt; T getData() throws ClassCastException {
          return (T) this.data;
      }
  }
  </code></pre>
  
  <p>Another Class:</p>
  
  <pre><code>public class StudentSignUpRequest {
  
       @NotNull(message = "First Name should not be empty")
       @Size(max = 64, message = "FirstName should not exceed 64 characters")
       private String firstName;
  
       @NotNull(message = "Last Name should not be empty")
       @Size(max = 64, message = "LastName should not exceed 64 characters")
       private String lastName;
  
       @NotNull(message = "Email cannot be empty")
       @Size(max = 50, message = "Email cannot exceed 50 characters")
       @Pattern(regexp = EMAIL_REGEX_PATTERN, message = "Email should contain a valid email address.")
       private String email;
  
       // other fields
  }
  </code></pre>
  
  <p>Controller Class:</p>
  
  <pre><code>@PostMapping(value = Constants.STUDENT_SIGN_UP)
  public Response signUpStudent(@Valid @RequestBody Request request, HttpServletRequest servletRequest) {
  
      // retrieving the actual resource from request payload
      StudentSignUpRequest signUpRequest = request.getData(StudentSignUpRequest.class);
      // call service to sign-up student
      return loginRegistrationService.signUpStudent(signUpRequest);
  }
  </code></pre>
  
  <p>Calling code sets request as below:</p>
  
  <pre><code>StudentSignUpRequest studentSignUpRequest = new StudentSignUpRequest();
  //setter methods
  
  Request payload = new Request();
  payload.setData(studentSignUpRequest);
  </code></pre>
  
  <p>This is the request I am sending:</p>
  
  <p>For more than 64 chars for firstName:</p>
  
  <p>Sample JSON:</p>
  
  <pre><code>{
      "data": {
          "firstName": "student111111111111111111111111111111111111111111111111111111111111",
          "lastName": "somesurname",
          "email": "developer@gmail.com"
      }
  }
  </code></pre>
  
  <p>Where first name not included:</p>
  
  <pre><code>{
      "data": {
          "lastName": "somesurname",
          "email": "developer@gmail.com"
      }
  }
  </code></pre>
  
  <p>Here both <code>@Size</code> as well as <code>@NotNull</code> annotation not working.</p>
  
  <p>Any solution?</p>