一、定义抽象异常类
import org.springframework.http.HttpStatus;
/**
* abstract http exception
*
* @author yang.liu
*/
public abstract class AbstractHttpException extends RuntimeException {
private static final long serialVersionUID = 1L;
private HttpStatus httpStatus;
public AbstractHttpException(String message) {
super(message);
}
public AbstractHttpException(String message, Exception e) {
super(message, e);
}
public HttpStatus getHttpStatus() {
return httpStatus;
}
public void setHttpStatus(HttpStatus httpStatus) {
this.httpStatus = httpStatus;
}
}
二、实现类
2.1 Bad Request Exception
import org.springframework.http.HttpStatus;
/**
* bad request exception
*
* @author yang.liu
*/
public class BadRequestException extends AbstractHttpException {
public BadRequestException(String message) {
super(message);
setHttpStatus(HttpStatus.BAD_REQUEST);
}
}
2.2 Not Foud Exception
import org.springframework.http.HttpStatus;
/**
* not foud exception
*
* @author yang.liu
*/
public class NotFoundException extends AbstractHttpException {
public NotFoundException(String message) {
super(message);
setHttpStatus(HttpStatus.NOT_FOUND);
}
}
2.3 Server Error Exception
import org.springframework.http.HttpStatus;
/**
* Service Error Exception
* @author yang.liu
*/
public class ServerErrorException extends AbstractHttpException {
public ServerErrorException(String message) {
super(message);
setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
}
public ServerErrorException(String message, Exception e) {
super(message, e);
setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
三、全局异常拦截处理
import lombok.Data;
import java.sql.Timestamp;
import java.time.LocalDateTime;
/**
* 错误响应
*
* @author yang.liu
*/
@Data
public class ErrorResponse {
private Long timestamp;
private Integer status;
private String exception;
private String message;
public ErrorResponse(Integer status, Exception exception) {
this(status, exception, exception.getMessage());
}
public ErrorResponse(Integer status, Exception exception, String message) {
this.timestamp = Timestamp.valueOf(LocalDateTime.now()).getTime();
this.status = status;
this.exception = exception.getClass().getName();
this.message = message;
}
}
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import java.util.List;
import static java.util.stream.Collectors.toList;
/**
* 全局异常处理
*
* @author yang.liu
*/
@Slf4j
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
/**
* 系统异常处理
*
* @param req
* @param e
* @return
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public ErrorResponse serverError(HttpServletRequest req, Exception e) {
log.error("error ", e.getMessage(), e);
return new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), e);
}
/**
* 处理自定义异常
*
* @param request
* @param ex
* @return
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({AbstractHttpException.class})
public ErrorResponse badRequest(HttpServletRequest request, AbstractHttpException ex) {
//打印日志
log.error("bad request error", ex.getMessage(), ex);
return new ErrorResponse(HttpStatus.BAD_REQUEST.value(), ex);
}
/**
* 参数校验失败
*
* @param e
* @return
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public ErrorResponse methodArgumentNotValidException(HttpServletRequest req, MethodArgumentNotValidException e) {
log.error("valid error ", e.getMessage(), e);
BindingResult result = e.getBindingResult();
List<String> errors = result.getFieldErrors().stream()
.map(error -> error.getField() + ":" + error.getDefaultMessage())
.collect(toList());
return new ErrorResponse(HttpStatus.BAD_REQUEST.value(), e, StringUtils.join(errors, ";"));
}
/**
* 参数校验失败
*
* @param e
* @return
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConstraintViolationException.class)
public ErrorResponse constraintViolationException(HttpServletRequest req, ConstraintViolationException e) {
log.error("violation error", e);
List<String> errors = e.getConstraintViolations().stream()
.map(error -> error.getMessage())
.collect(toList());
return new ErrorResponse(HttpStatus.BAD_REQUEST.value(), e, StringUtils.join(errors, ";"));
}
}
四、使用示例
if(user == null){
throw new BadRequestException("用户不存在");
}