1 前后端统一数据交互方式
1.1 统一结果集
package com.zhmsky.result;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
@Data
public class Result<T> implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "成功标志")
private boolean success;
@ApiModelProperty(value = "消息")
private String message;
@ApiModelProperty(value = "返回代码")
private Integer code;
@ApiModelProperty(value = "时间戳")
private long timestamp = System.currentTimeMillis();
@ApiModelProperty(value = "结果对象")
private T result;
}
1.2 结果集返回工具类
package com.zhmsky.result;
public class ResultUtil<T> {
private Result<T> result;
public ResultUtil() {
result = new Result<>();
result.setSuccess(true);
result.setMessage("success");
result.setCode(200);
}
public Result<T> setData(T t) {
this.result.setResult(t);
this.result.setCode(200);
return this.result;
}
public Result<T> setSuccessMsg(String msg) {
this.result.setSuccess(true);
this.result.setMessage(msg);
this.result.setCode(200);
this.result.setResult(null);
return this.result;
}
public Result<T> setData(T t, String msg) {
this.result.setResult(t);
this.result.setCode(200);
this.result.setMessage(msg);
return this.result;
}
public Result<T> setErrorMsg(String msg) {
this.result.setSuccess(false);
this.result.setMessage(msg);
this.result.setCode(500);
return this.result;
}
public Result<T> setErrorMsg(Integer code, String msg) {
this.result.setSuccess(false);
this.result.setMessage(msg);
this.result.setCode(code);
return this.result;
}
public static <T> Result<T> data(T t) {
return new ResultUtil<T>().setData(t);
}
public static <T> Result<T> data(T t, String msg) {
return new ResultUtil<T>().setData(t, msg);
}
public static <T> Result<T> success(String msg) {
return new ResultUtil<T>().setSuccessMsg(msg);
}
public static <T> Result<T> error(String msg) {
return new ResultUtil<T>().setErrorMsg(msg);
}
public static <T> Result<T> error(Integer code, String msg) {
return new ResultUtil<T>().setErrorMsg(code, msg);
}
}
2 异常统一处理
2.1 全局异常处理
package com.zhmsky.exception.handler;
import com.zhmsky.exception.MyException;
import com.zhmsky.result.Result;
import com.zhmsky.result.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
@ControllerAdvice
public class ExceptionHandler {
@ResponseBody
@org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
public Result<String> error(Exception e){
e.printStackTrace();
return new ResultUtil<String>().setErrorMsg("发生未知错误!请联系管理员");
}
}
2.2 特定异常处理
package com.zhmsky.exception.handler;
import com.zhmsky.exception.MyException;
import com.zhmsky.result.Result;
import com.zhmsky.result.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
@ControllerAdvice
public class ExceptionHandler {
@ResponseBody
@org.springframework.web.bind.annotation.ExceptionHandler(ArithmeticException.class)
public Result<String> error(ArithmeticException e){
e.printStackTrace();
return new ResultUtil<String>().setErrorMsg("算数异常!请重新输入!");
}
}
2.3 自定义异常处理
2.3.1 自定义异常类
package com.zhmsky.exception;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyException extends RuntimeException{
@ApiModelProperty("状态码")
private Integer code;
@ApiModelProperty("异常消息")
private String msg;
}
自定义异常需要在程序可能出现异常处手动抛出,例:
try {
int i = 1 / 0;
} catch (Exception e) {
throw new MyException(20002,"自定义异常");
}
2.3.2 异常处理
package com.zhmsky.exception.handler;
import com.zhmsky.exception.MyException;
import com.zhmsky.result.Result;
import com.zhmsky.result.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
@ControllerAdvice
public class ExceptionHandler {
@ResponseBody
@org.springframework.web.bind.annotation.ExceptionHandler(MyException.class)
public Result<Map<String,Object>> error(MyException e){
e.printStackTrace();
return new ResultUtil<Map<String,Object>>().setErrorMsg(e.getMsg());
}
}
|