原由
在程序开发时,客户端与服务端进行交互的过程中,客户端需要对服务端返回的异常进行处理然后提示给用户该过程出了什么错误。因此服务端如何封装好程序运行过程中出现的异常与错误就显得尤为的重要。
解决方案
使用@ControllerAdvice
注解进行全局异常处理
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = {Exception.class})
protected ResponseEntity<Object> handleInvalidRequest(Exception ex) {
if (ex instanceof CommonException) {
return ResponseEntity
.status(((CommonException) ex).getHttpStatusCode())
.body(((CommonException) ex).getHttpResponse());
} else if (ex instanceof MethodArgumentNotValidException) {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(CodeEnum.PARAMETER_INVALID);
}
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("internal server error");
}
}
或者继承ResponseEntityExceptionHandler
,然后重写其提供的方法或补充自定义方法
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = {CommonException.class, Exception.class})
protected ResponseEntity<Object> handleInvalidRequest(Exception ex, HttpServletRequest request) {
if (ex instanceof CommonException) {
return ResponseEntity
.status(((CommonException) ex).getHttpStatusCode())
.body(((CommonException) ex).getHttpResponse());
}
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR).body("internal server error");
}
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request
) {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(HttpResponse.fail(CodeEnum.PARAMETER_INVALID));
}
}
封装错误码
- 接口
public interface ICodeEnum {
Integer getErrorCode();
Integer getHttpStatusCode();
String getErrorMessage();
}
- 类
public enum CodeEnum implements ICodeEnum {
/**
* parameter invalid
*/
PARAMETER_INVALID(100001, "parameter invalid", HttpStatus.BAD_REQUEST.value());
CodeEnum(Integer errorCode, String errorMessage, Integer httpStatusCode) {
this.errorCode = errorCode;
this.errorMessage = errorMessage;
this.httpStatusCode = httpStatusCode;
}
private Integer httpStatusCode;
private Integer errorCode;
private String errorMessage;
@Override
public Integer getErrorCode() {
return this.errorCode;
}
@Override
public Integer getHttpStatusCode() {
return this.httpStatusCode;
}
@Override
public String getErrorMessage() {
return this.errorMessage;
}
}
封装统一返回结果
public class HttpResult<T> implements Serializable {
private static final long serialVersionUID = -6095962827814337775L;
private Integer code;
private String message;
private T data;
public HttpResult() {
this.message = "";
this.code = HttpStatus.OK.value();
}
private HttpResult(T data) {
this.data = data;
this.code = HttpStatus.OK.value();
this.message = "";
}
private HttpResult(Integer code, String message) {
this.code = code;
this.message = message;
}
public static <T extends Serializable> HttpResult<T> success() {
return new HttpResult<>();
}
public static <T extends Serializable> HttpResult<T> success(T data) {
return new HttpResult<>(data);
}
public static <T extends Serializable> HttpResult<T> fail(Integer code) {
return new HttpResult<>(code, "");
}
public static <T extends Serializable> HttpResult<T> fail(Integer code, String message) {
return new HttpResult<>(code, message);
}
public static <T extends Serializable> HttpResult<T> fail(ICodeEnum codeEnum) {
return new HttpResult<>(codeEnum.getErrorCode(), codeEnum.getErrorMessage());
}
}
controller层
@RestController
public class AController {
@PostMapping("/a")
public void store() {
throw new CommonException(CodeEnum.PARAMETER_INVALID);
}
}