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" }

1
  • check this question, especially the answer from Geoff Bourne. might help. Commented Mar 30, 2017 at 7:01

1 Answer 1

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);
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.