title: SpringBoot DTO Validation
body: <p>I am new to spring-boot I'm trying to add validation to my DTO class like below.</p>
  
  <pre><code>import javax.validation.constraints.NotBlank;
  
  @Getter
  @Setter
  public class EmployeeDto {
      private Long id;
  
      @NotBlank(message = "Employee first name is required")
      private String firstName;
  
      private String lastName;
  
      @NotBlank(message = "EmployeeNUM  is required")
      private String employeeNum;
  
  }
  </code></pre>
  
  <p>Below is my REST endpoint to save employee.</p>
  
  <pre><code>import javax.validation.Valid;
   @PostMapping("/employee")
      public ResponseEntity&lt;?&gt; addEmployee(@Valid @RequestBody EmployeeDto employeeDto) throws ClassNotFoundException {
          return   ResponseEntity.ok(employeeService.saveEmployee(deptId,employeeDto));
  
      }
  </code></pre>
  
  <p>I create a Validation class like below to validate the DTO fields.</p>
  
  <pre><code>@ControllerAdvice
  @RestController
  public class Validation {
  
      @ResponseStatus(HttpStatus.BAD_REQUEST)
      @ExceptionHandler(MethodArgumentNotValidException.class)
      public Map&lt;String, String&gt; handleValidationExceptions(
              MethodArgumentNotValidException ex) {
          Map&lt;String, String&gt; errors = new HashMap&lt;&gt;();
          ex.getBindingResult().getAllErrors().forEach((error) -&gt; {
              String fieldName = ((FieldError) error).getField();
              String errorMessage = error.getDefaultMessage();
              errors.put(fieldName, errorMessage);
          });
          return errors;
  
      }
  }
  </code></pre>
  
  <p>expected output is </p>
  
  <p>{
    "firstName":"Employee first name is required",
    "employeeNum":"EmployeeNUM is required"
  }</p>
  
  <p>But I'm getting only the 400 bad request when hit the endpoint through postman.
  What is the issue with my code? How to fix and get the expected output as mentioned above?</p>