在这里首先感谢的就是程序员老罗,从他的项目里面学到了这些东西。
首先就是去创建一个SpringBoot项目,这里我就不多做赘述了
封装一个统一返回对象
package com.example.demo.vo;
public class ResponseVO<T> {
private String status;
private Integer code;
private String info;
private T data;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
封装一个异常返回对象
package com.example.demo.exception;
import com.example.demo.entity.enums.ResponseCodeEnum;
public class BusinessException extends RuntimeException {
private ResponseCodeEnum codeEnum;
private Integer code;
private String message;
public BusinessException(String message, Throwable e) {
super(message, e);
this.message = message;
}
public BusinessException(String message) {
super(message);
this.message = message;
}
public BusinessException(Throwable e) {
super(e);
}
public BusinessException(ResponseCodeEnum codeEnum) {
super(codeEnum.getMsg());
this.codeEnum = codeEnum;
this.code = codeEnum.getCode();
this.message = codeEnum.getMsg();
}
public BusinessException(Integer code, String message) {
super(message);
this.code = code;
this.message = message;
}
public ResponseCodeEnum getCodeEnum() {
return codeEnum;
}
public Integer getCode() {
return code;
}
@Override
public String getMessage() {
return message;
}
/**
* 重写fillInStackTrace 业务异常不需要堆栈信息,提高效率.
*/
@Override
public Throwable fillInStackTrace() {
return this;
}
}
定义一个请求结果的枚举
package com.example.demo.entity.enums;
public enum ResponseCodeEnum {
CODE_200(200,"请求成功"),
CODE_404(404,"请求地址不存在"),
CODE_600(600,"请求参数错误"),
CODE_601(601,"信息已经存在"),
CODE_500(500,"服务器返回错误,请联系管理员");
private Integer code;
private String msg;
ResponseCodeEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
封装一个统一的返回方法(正确,错误,异常)
package com.example.demo.controller;
import com.example.demo.entity.enums.ResponseCodeEnum;
import com.example.demo.exception.BusinessException;
import com.example.demo.vo.ResponseVO;
public class ABaseController {
protected static final String STATUS_SUCCESS = "success";
protected static final String STATUS_ERROR = "error";
protected <T> ResponseVO getSuccessResponseVO(T t){
ResponseVO<T> responseVO = new ResponseVO<>();
responseVO.setStatus(STATUS_SUCCESS);
responseVO.setCode(ResponseCodeEnum.CODE_200.getCode());
responseVO.setInfo(ResponseCodeEnum.CODE_200.getMsg());
responseVO.setData(t);
return responseVO;
}
protected <T> ResponseVO getBusinessErrorResponseVO(BusinessException e,T t){
ResponseVO vo = new ResponseVO<>();
vo.setStatus(STATUS_ERROR);
if(e.getCode() == null){
vo.setCode(ResponseCodeEnum.CODE_600.getCode());
}else{
vo.setCode(e.getCode());
}
vo.setInfo(e.getMessage());
vo.setData(t);
return vo;
}
protected <T> ResponseVO getServerErrorResponseVO(T t){
ResponseVO vo = new ResponseVO<>();
vo.setStatus(STATUS_ERROR);
vo.setCode(ResponseCodeEnum.CODE_500.getCode());
vo.setInfo(ResponseCodeEnum.CODE_500.getMsg());
vo.setData(t);
return vo;
}
}
定义一个全局的异常处理器
package com.example.demo.controller;
import com.example.demo.entity.enums.ResponseCodeEnum;
import com.example.demo.exception.BusinessException;
import com.example.demo.vo.ResponseVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.yaml.snakeyaml.constructor.DuplicateKeyException;
import javax.servlet.http.HttpServletRequest;
@RestControllerAdvice
public class AGlobalExceptionHandlerController extends ABaseController {
private static final Logger logger = LoggerFactory.getLogger(AGlobalExceptionHandlerController.class);
@ExceptionHandler(value = Exception.class)
public Object handleException(Exception e, HttpServletRequest request){
logger.error("请求错误,请求地址{},错误信息:", request.getRequestURL(), e);
ResponseVO ajaxResponse = new ResponseVO();
//404
if (e instanceof NoHandlerFoundException) {
ajaxResponse.setCode(ResponseCodeEnum.CODE_404.getCode());
ajaxResponse.setInfo(ResponseCodeEnum.CODE_404.getMsg());
ajaxResponse.setStatus(STATUS_ERROR);
} else if (e instanceof BusinessException) {
//业务错误
BusinessException biz = (BusinessException) e;
ajaxResponse.setCode(biz.getCode() == null ? ResponseCodeEnum.CODE_600.getCode() : biz.getCode());
ajaxResponse.setInfo(biz.getMessage());
ajaxResponse.setStatus(STATUS_ERROR);
} else if (e instanceof BindException || e instanceof MethodArgumentTypeMismatchException) {
//参数类型错误
ajaxResponse.setCode(ResponseCodeEnum.CODE_600.getCode());
ajaxResponse.setInfo(ResponseCodeEnum.CODE_600.getMsg());
ajaxResponse.setStatus(STATUS_ERROR);
} else if (e instanceof DuplicateKeyException) {
//主键冲突
ajaxResponse.setCode(ResponseCodeEnum.CODE_601.getCode());
ajaxResponse.setInfo(ResponseCodeEnum.CODE_601.getMsg());
ajaxResponse.setStatus(STATUS_ERROR);
} else {
ajaxResponse.setCode(ResponseCodeEnum.CODE_500.getCode());
ajaxResponse.setInfo(ResponseCodeEnum.CODE_500.getMsg());
ajaxResponse.setStatus(STATUS_ERROR);
}
return ajaxResponse;
}
}
最后进行测试
package com.example.demo.controller;
import com.example.demo.entity.enums.ResponseCodeEnum;
import com.example.demo.exception.BusinessException;
import com.example.demo.vo.ResponseVO;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import org.yaml.snakeyaml.constructor.DuplicateKeyException;
@RestController
public class hello extends ABaseController {
@RequestMapping("/hello")
public void hello() {
throw new BusinessException(ResponseCodeEnum.CODE_404);
}
}