title: Spring Boot how to return my own validation constraint error messages
body: <p>I need to have my own error response body when something goes wrong with my request and I am trying to use the <code>@NotEmpty</code> constraint message attribute to return the error message,</p>
  
  <p>This is my class that returns the error message using the body that I need:</p>
  
  <pre><code>package c.m.nanicolina.exceptions;
  
  
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.MissingServletRequestParameterException;
  import org.springframework.web.bind.annotation.ControllerAdvice;
  import org.springframework.web.bind.annotation.ExceptionHandler;
  import org.springframework.web.context.request.WebRequest;
  
  @ControllerAdvice
  public class CustomResponseEntityExceptionHandler {
  
      @ExceptionHandler(value = {MissingServletRequestParameterException.class})
      public ResponseEntity&lt;ApiError&gt; handleConflict(MissingServletRequestParameterException ex, WebRequest request) {
          ApiError apiError = new ApiError(ex.getMessage(), ex.getMessage(), 1000);
          return new ResponseEntity&lt;ApiError&gt;(apiError, null, HttpStatus.BAD_REQUEST);
      }
  }
  </code></pre>
  
  <p>With this <code>CustomResponseEntityExceptionHandler</code> I can return my own response body in case of validation errors.</p>
  
  <p>What I am trying now is to get the message from the validation constraints.</p>
  
  <p>This is my controller with the <code>NotEmpty</code> constraint:</p>
  
  <pre><code>package c.m.nanicolina.controllers;
  
  import c.m.nanicolina.models.Product;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.bind.annotation.RestController;
  
  import javax.validation.constraints.NotEmpty;
  
  @RestController
  public class MinimumStockController {
  
      @RequestMapping(value = "/minimumstock")
      public Product product(
              @RequestParam(value = "product.sku") @NotEmpty(message = "Product.sku cannot be empty") String sku,
              @RequestParam(value = "stock.branch.id") String branchID) {
          return null;
      }
  }
  </code></pre>
  
  <p>In my exception, <strong>I can't find a way to get that message <code>Product.sku cannot be empty</code> and show it in my error response.</strong></p>
  
  <p>I have also checked the class <code>MissingServletRequestParameterException</code> and there is the method <code>getMessage</code> which is returning the default message.</p>