0

Hi i need to throw custom errors with custom status.The response status should be always 200 /201.Please help me to reslove this issue.Current status in given below

{ "timestamp": "2017-03-30T05:59:56.010+0000", "status": 400, "error": "Bad Request", "exception": "custom exception", "message": "custom message", "path": "/api/sjsdj" } What i expect as response is

{ "timestamp": "2017-03-30T05:59:56.010+0000", "status": 600, "error": "custom error", "exception": "web.rest.errors.custom exception", "message": "custom message", "path": "/api/hgdsh/3" }

user7472292
  • 65
  • 1
  • 4
  • check [this question](http://stackoverflow.com/questions/26236811/spring-boot-customize-http-error-response), especially the answer from Geoff Bourne. might help. – P.J.Meisch Mar 30 '17 at 07:01

1 Answers1

0

You can use @ExceptionHandler.

Create a @ControllerAdvice with methods annotated with @ExceptionHandler that cache your custom exceptions / generic one and return your custom status/message etc there.

Example:

@ControllerAdvice
public class ControllerExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    ResponseEntity<Object> handleControllerException(HttpServletRequest req, Throwable ex) {
        ErrorResponse errorResponse = new ErrorResponse(ex);
        return new ResponseEntity<Object>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
    }

}
Tom
  • 3,711
  • 2
  • 25
  • 31