springbootweb全局異常@RestControllerAdvice
1.WebControllerAdvice
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.UNAUTHORIZED;
import static org.springframework.http.HttpStatus.valueOf;
import java.util.stream.Collectors;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import com.demo.exceptions.HeaderValidationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import feign.FeignException;
@Slf4j
@RestControllerAdvice("com.demo")
public class WebControllerAdvice {
private static final String ERROR_FIELD = "error";
@Autowired
protected ObjectMapper objectMapper;
@Autowired
public WebControllerAdvice(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
private static final String SOMETHING_WENT_WRONG_MSG = "Sorry, something went wrong. Please contact your local HSBC Bank";
private static final String UNAUTHORIZED_MSG = "Unauthorized";
private static final String CUSTOMER_NOT_FOUND_MSG = "Customer Not Found";
private static final String METHOD_NOT_ALLOWED = "Method Not Allowed";
private static final String ERROR_TEMPLATE = "ERROR: type={} statuscode={} cause={} message={}";
// Thrown by feign and assessed
@ExceptionHandler(feign.FeignException.class)
public ResponseEntity<JsonNode> handleFeignException(final FeignException ex) {
log.error(ERROR_TEMPLATE, ex.getClass().getName(), ex.status(), ex.getCause(), ex.getMessage());
switch (ex.status()) {
case 404:
return new ResponseEntity<JsonNode>(msg(CUSTOMER_NOT_FOUND_MSG), NOT_FOUND);
case 403:
return new ResponseEntity<JsonNode>(msg(UNAUTHORIZED_MSG), UNAUTHORIZED);
case 405:
return new ResponseEntity<JsonNode>(msg(METHOD_NOT_ALLOWED), HttpStatus.METHOD_NOT_ALLOWED);
default:
return new ResponseEntity<JsonNode>(msg(SOMETHING_WENT_WRONG_MSG), ex.status() == 0 ? HttpStatus.INTERNAL_SERVER_ERROR
: valueOf(ex.status()));
}
}
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public ResponseEntity<JsonNode> handleHttpMediaTypeNotSupportedException(final ValidationException ex) {
log.error(ERROR_TEMPLATE, ex.getClass().getName(), HttpStatus.BAD_REQUEST, ex.getCause(),
ex.getMessage());
return new ResponseEntity<JsonNode>(msg(SOMETHING_WENT_WRONG_MSG), valueOf(BAD_REQUEST.name()));
}
@ExceptionHandler(ValidationException.class)
public ResponseEntity<JsonNode> handleValidationException(final ValidationException ex) {
log.error(ERROR_TEMPLATE, ex.getClass().getName(), HttpStatus.BAD_REQUEST, ex.getCause(),
ex.getMessage());
return new ResponseEntity<JsonNode>(msg(SOMETHING_WENT_WRONG_MSG), valueOf(BAD_REQUEST.name()));
}
@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<JsonNode> handleConstraintViolationException(final ConstraintViolationException ex) {
log.error(ERROR_TEMPLATE, ex.getClass().getName(), HttpStatus.BAD_REQUEST, ex.getCause(), ex
.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining()));
return new ResponseEntity<JsonNode>(msg(SOMETHING_WENT_WRONG_MSG), valueOf(BAD_REQUEST.name()));
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<JsonNode> handleMethodArgumentNotValidException(final MethodArgumentNotValidException ex) {
log.error(ERROR_TEMPLATE, ex.getClass().getName(), BAD_REQUEST, ex.getCause(), ex.getMessage());
return new ResponseEntity<JsonNode>(msg(SOMETHING_WENT_WRONG_MSG), BAD_REQUEST);
}
@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<JsonNode> handleHttpMessageNotReadableException(final HttpMessageNotReadableException ex) {
log.error(ERROR_TEMPLATE, ex.getClass().getName(), BAD_REQUEST, ex.getCause(), ex.getMessage());
return new ResponseEntity<JsonNode>(msg(SOMETHING_WENT_WRONG_MSG), BAD_REQUEST);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<JsonNode> handleBusinessException(final Exception ex) {
log.error(ERROR_TEMPLATE, ex.getClass().getName(), ex.getCause(), ex.getMessage());
return new ResponseEntity<JsonNode>(msg(SOMETHING_WENT_WRONG_MSG), INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<JsonNode> handleHttpRequestMethodNotSupportedException(final HttpRequestMethodNotSupportedException ex) {
log.error(ERROR_TEMPLATE, ex.getClass().getName(), ex.getCause(), ex.getMessage());
return new ResponseEntity<JsonNode>(msg(METHOD_NOT_ALLOWED), BAD_REQUEST);
}
@ExceptionHandler(HeaderValidationException.class)
public ResponseEntity<JsonNode> handleHeaderValidationException(final HeaderValidationException headerValidationException) {
log.error(ERROR_TEMPLATE, headerValidationException.getClass().getName(),
headerValidationException.getCause(), headerValidationException.getMessage());
return new ResponseEntity<JsonNode>(msg(headerValidationException.getMessage()), HttpStatus.BAD_REQUEST);
}
private JsonNode msg(final String errorMessage) {
return this.objectMapper.createObjectNode().put(ERROR_FIELD, errorMessage);
}
}
2.自定義異常類
HeaderValidationException
import lombok.Data;@Data
public class HeaderValidationException extends RuntimeException {
public HeaderValidationException(final String message) {
super(message);
}
public HeaderValidationException(final String message, final Throwable cause) {
super(message, cause);
}
public HeaderValidationException(final Throwable cause) {
super(cause);
}
}
以上是 springbootweb全局異常@RestControllerAdvice 的全部内容, 来源链接: utcz.com/z/511851.html