Open jabrena opened 8 years ago
https://www.jayway.com/2014/10/19/spring-boot-error-responses/
@ExceptionHandler(IllegalArgumentException.class)
void handleBadRequests(HttpServletResponse response) throws IOException {
response.sendError(HttpStatus.BAD_REQUEST.value(), "Please try again and with a non empty string as 'name'");
}
http://briansjavablog.blogspot.co.uk/2015/12/spring-boot-rest-tutorial.html
package com.blog.samples.boot.rest.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@ControllerAdvice
public class ControllerExceptionHandler {
@ResponseStatus(HttpStatus.NOT_FOUND) // 404
@ExceptionHandler(CustomerNotFoundException.class)
public void handleNotFound() {
log.error("Resource not found");
}
@ResponseStatus(HttpStatus.BAD_REQUEST) // 400
@ExceptionHandler(InvalidCustomerRequestException.class)
public void handleBadRequest() {
log.error("Invalid Fund Request");
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 500
@ExceptionHandler(Exception.class)
public void handleGeneralError(Exception ex) {
log.error("An error occurred procesing request", ex);
}
}
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
@ExceptionHandler(value = RuntimeException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public RestEntity handleException(HttpServletRequest req, RuntimeException ex) {
RestEntity restEntity=new RestEntity();
Message message=new Message();
message.setCode(1000);
message.setMessage("Something wrong with the server");
restEntity.setMessage(message);
return restEntity;
}
}