统一信息返回和全局异常工具类
需要一个用于存储返回消息体和自定义的异常消息
package com.dome.common.utils.model; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.NoArgsConstructor; /** * @author YinXiaoWei * @date 2020/11/17 15:35 * 异常错误码 */ @Data @NoArgsConstructor @ApiModel(value = "异常错误码", description = "") public class CodeMsg { @ApiModelProperty(value = "响应码") private Integer code; @ApiModelProperty(value = "响应消息") private String msg; public static CodeMsg SERVER_ERROR = new CodeMsg(100001, "服务端异常,程序员小哥正在维护中!"); public static CodeMsg BIND_ERROR = new CodeMsg(100002, "参数校验异常!"); public static CodeMsg NOT_METHOD_SUPPORT = new CodeMsg(10003, "错误的请求方式!"); public static CodeMsg NOT_ALLOWED_API = new CodeMsg(200001, "非法第三方调用来源"); public static CodeMsg ERROR_TRANS_DATA = new CodeMsg(200002, "非法交易体(transData)"); public static CodeMsg NULL_TRANS_SIGN = new CodeMsg(200003, "加密串(sign)不能为空"); public static CodeMsg NOT_ALLOW_SIGN = new CodeMsg(200004, "加密字符串不合法"); public static CodeMsg NULL_TRANS_DATA = new CodeMsg(200005, "交易体(transData)不能为空"); public static CodeMsg NULL_TRANS_NO = new CodeMsg(200006, "交易号(transNo)不能为空"); public static CodeMsg ERROR_TRANS_NO = new CodeMsg(200007, "非法签约交易号(transNo)"); public static CodeMsg NOT_PARAMETER = new CodeMsg(300001, "缺少关键参数字段!"); public static CodeMsg NOT_NULL = new CodeMsg(300002, "未查询到数据!"); public CodeMsg(Integer code, String msg) { this.code = code; this.msg = msg; } public CodeMsg(String msg) { this.code = 500; this.msg = msg; } public CodeMsg fillArgs(Object... args) { int code = this.code; String message = String.format(this.msg, args); return new CodeMsg(code, message); } }
统一信息返回体
package com.dome.common.utils.model; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; import java.io.Serializable; /** * @author YinXiaoWei * @date 2020/11/17 15:29 * 统一响应类 */ @Data @NoArgsConstructor @Accessors(chain = true) public class ResponseModel<T> implements Serializable { @ApiModelProperty(value = "响应码") private Integer code; @ApiModelProperty(value = "响应消息") private String msg; @ApiModelProperty(value = "响应数据") private T data; public static ResponseModel success(Object data){ return new ResponseModel(data); } public static ResponseModel success(String msg, Object data) { return new ResponseModel(msg, data); } public static ResponseModel success(String msg) { return new ResponseModel(msg); } public static ResponseModel error(CodeMsg cm){ return new ResponseModel(cm); } public static ResponseModel error(Integer code, String msg){ return new ResponseModel(code, msg); } public static ResponseModel error(String msg){ return new ResponseModel(500, msg); } private ResponseModel(T data) { this.code=0; this.msg="success"; this.data=data; } private ResponseModel(String msg) { this.code = 0; this.msg = msg; } private ResponseModel(CodeMsg cm) { if(cm == null){ return; } this.code = cm.getCode(); this.msg = cm.getMsg(); } private ResponseModel(String msg, T data) { this.code = 0; this.msg = msg; this.data = data; } private ResponseModel(Integer code, String msg, T data) { this.code = code; this.msg = msg; this.data = data; } private ResponseModel(Integer code, String msg) { this.code = code; this.msg = msg; } }
统一异常处理类
package com.dome.common.utils.exception; import com.dome.common.utils.model.CodeMsg; import java.text.MessageFormat; /** * @author YinXiaoWei * @date 2020/11/25 14:09 */ public class GlobalException extends RuntimeException{ private static final long serialVersionUID = 1L; private CodeMsg codeMsg; public GlobalException(CodeMsg codeMsg) { super(codeMsg.toString()); this.codeMsg = codeMsg; } public GlobalException(CodeMsg codeMsg, Object... arguments) { this.codeMsg = new CodeMsg(codeMsg.getCode(), arguments != null && arguments.length != 0 ? MessageFormat.format(codeMsg.getMsg(), arguments) : MessageFormat.format(codeMsg.getMsg(), "")); } public GlobalException(Integer code, String msg) { this.codeMsg = new CodeMsg(code, msg); } public GlobalException(String msg) { this.codeMsg = new CodeMsg(msg); } public CodeMsg getCm() { return codeMsg; } public void setCodeMsg(CodeMsg codeMsg) { this.codeMsg = codeMsg; } }
全局捕获异常
package com.dome.common.utils.exception; import com.dome.common.utils.model.CodeMsg; import com.dome.common.utils.model.ResponseModel; import lombok.extern.slf4j.Slf4j; import org.springframework.validation.BindException; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.web.HttpRequestMethodNotSupportedException; 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.ResponseBody; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Set; /** * @author YinXiaoWei * @date 2020/11/25 11:08 */ @Slf4j @ControllerAdvice @ResponseBody public class GlobalExceptionHandler { /** * 请求方式不支持 */ @ExceptionHandler({HttpRequestMethodNotSupportedException.class}) public ResponseModel handleException(HttpRequestMethodNotSupportedException e) { log.error(e.getMessage(), e); return ResponseModel.error(CodeMsg.NOT_METHOD_SUPPORT); } /** * 拦截未知的运行时异常 */ @ExceptionHandler(RuntimeException.class) public ResponseModel notFount(RuntimeException e) { log.error("运行时异常:", e); return ResponseModel.error(10001, e.getMessage()); } /** * 系统异常 */ @ExceptionHandler(Exception.class) public ResponseModel handleException(Exception e) { log.error(e.getMessage(), e); return ResponseModel.error(CodeMsg.SERVER_ERROR); } /** * 校验异常 */ @ExceptionHandler(value = MethodArgumentNotValidException.class) public ResponseModel exceptionHandler(MethodArgumentNotValidException e) { BindingResult bindingResult = e.getBindingResult(); String errorMessage = ""; for (FieldError fieldError : bindingResult.getFieldErrors()) { errorMessage += fieldError.getDefaultMessage() + "!"; } return ResponseModel.error(500, errorMessage); } /** * 校验异常 */ @ExceptionHandler(value = BindException.class) public ResponseModel validationExceptionHandler(BindException e) { BindingResult bindingResult = e.getBindingResult(); String errorMessage = ""; for (FieldError fieldError : bindingResult.getFieldErrors()) { errorMessage += fieldError.getDefaultMessage() + "!"; } return ResponseModel.error(500, errorMessage); } /** * 校验异常 */ @ExceptionHandler(value = ConstraintViolationException.class) public ResponseModel constraintViolationExceptionHandler(ConstraintViolationException ex) { Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations(); Iterator<ConstraintViolation<?>> iterator = constraintViolations.iterator(); List<String> msgList = new ArrayList<>(); while (iterator.hasNext()) { ConstraintViolation<?> cvl = iterator.next(); msgList.add(cvl.getMessageTemplate()); } return ResponseModel.error(500, String.join(",",msgList)); } /** * 业务异常 */ @ExceptionHandler(GlobalException.class) public ResponseModel businessException(GlobalException e) { log.error(e.getMessage(), e); GlobalException ex = (GlobalException) e; return ResponseModel.error(ex.getCm()); } }