Spring boot WebService Rest (CUSTOMIZE RESPONSE)

3

I want to customize the answer when it is a Status 405 Method Not Allowed

{
    "timestamp": 1527270306717,
    "status": 405,
    "error": "Method Not Allowed",
    "exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
    "message": "Request method 'POST' not supported",
    "path": "/students/Student1/courses"
}

That's the default answer and I would like to customize it and I used this and it does not work

@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public String handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
    System.out.println("cualquier cosa para saber si pasa por aqui");
    return null;
}

On the other hand I have another Exceptionhandler and in this if it enters when it is another code like 409 or 400, and the 405 does not

@ExceptionHandler({RuntimeException.class,HttpRequestMethodNotSupportedException.class})
    public ResponseEntity<ErrorDTO> processRuntimeException(HttpServletRequest req, RuntimeException ex) throws Exception {

    return null;
}

Does anyone know how to solve it?

    
asked by Chriz CR 25.05.2018 в 19:58
source

1 answer

0

Of course you can customize your own error message with the following steps:

  • You must create a personalized error message which you want to show.

    public class MensageDeError {
    
    
    private HttpStatus status;
    private String mensaje;
    private List<String> errores;
    
    public MensageDeError(HttpStatus status, String mensaje, List<String> errores) {
        super();
        this.status = status;
        this.mensaje = mensaje;
        this.errores = errores;
    }
    
    public MensageDeError(HttpStatus status, String mensaje, String error) {
        super();
        this.status = status;
        this.mensaje = mensaje;
        errores = Arrays.asList(error);
    }
    

    }

  • You must create a customizable exception handler to handle the exception that is emerging in this case is HttpRequestMethodNotSupportedException.

        @ControllerAdvice     public class CustomizableExceptions Handler extends ResponseEntityExceptionHandler {         @Override         protected ResponseEntity handleHttpRequestMethodNotSupported (                 HttpRequestMethodNotSupportedException ex,                 HttpHeaders headers,                 HttpStatus status,                 WebRequest request) {             StringBuilder constructor = new StringBuilder ();             constructor.append (ex.getMethod ());             constructor.append (                     "method is not supported for this request. Supported methods are:");             ex.getSupportedHttpMethods (). forEach (t -> constructor.append (t + ""));

            ErrorError_Email = new ErrorError (HttpStatus.METHOD_NOT_ALLOWED,                 ex.getLocalizedMessage (), constructor.toString ());         return new ResponseEntity < Object > (                 errorEmail, new HttpHeaders (), errorEmail.getStatus ());     } }

  • answered by 30.10.2018 в 01:04