相信全局统一格式返回这个东西在每个项目中都非常重要的,前端需要一个统一的格式给前端,所以我们后端需要封装好结构给前端。
相信全局统一格式返回这个东西在每个项目中都非常重要的,前端需要一个统一的格式给前端,所以我们后端需要封装好结构给前端。
我目前接触的结构基本上是这样的。
{
"code":"0",
"msg":"请求正常",
"data":{}
}
2实现
创建一个统一返回前端的实体类
public class R extends HashMap3异常处理{ private static final long serialVersionUID = 1L; public static final String CODE_TAG = "code"; public static final String MSG_TAG = "msg"; public static final String DATA_TAG = "data"; public R() { } public R(String code, String msg) { super.put(CODE_TAG, code); super.put(MSG_TAG, msg); } public R(String code, String msg, Object data) { super.put(CODE_TAG, code); super.put(MSG_TAG, msg); if (null!=data) { super.put(DATA_TAG, data); } } @Override public R put(String key, Object value) { super.put(key, value); return this; } public static R success() { return R.success("操作成功"); } public static R success(Object data) { return R.success("操作成功", data); } public static R success(String msg) { return R.success(msg, null); } public static R success(String msg, Object data) { return new R("0", msg, data); } public static R error() { return R.error("操作失败"); } public static R error(String msg) { return R.error("-1", msg); } public static R error(String msg, Object data) { return new R("-1", msg, data); } public static R error(String code, String msg) { return new R(code, msg, null); } }
正常情况下的返回结构已经弄好了,异常情况下我们也要做处理的。
首先新建几个异常类,根据实际需要创建。
@Getter
public class baseException extends RuntimeException {
private static final long serialVersionUID = 1L;
private final String module;
private final String code;
private final Object[] args;
private final String message;
public baseException(String module, String code, Object[] args, String message) {
this.module = module;
this.code = code;
this.args = args;
this.message = message;
}
public baseException(String module, String code, Object[] args) {
this(module, code, args, null);
}
public baseException(String module, String defaultMessage) {
this(module, null, null, defaultMessage);
}
public baseException(String code, Object[] args) {
this(null, code, args, null);
}
public baseException(String module, String code, String message) {
this(null, code, null, message);
}
public baseException(String message) {
this(null, null, null, message);
}
public String getCode() {
return code;
}
public String getMsg() { return message; }
public Object[] getArgs() {
return args;
}
public String getDefaultMessage() { return getMessage(); }
}
public class CustomException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String code;
private final String message;
public CustomException(String message) {
this.message = message;
}
public CustomException(String message, String code) {
this.message = message;
this.code = code;
}
public CustomException(String message, Throwable e) {
super(message, e);
this.message = message;
}
@Override
public String getMessage() {
return message;
}
public String getCode() {
return code;
}
}
我就创建了两个异常类。
4创建全局异常拦截器@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(baseException.class)
public R baseException(baseException e) {
return R.error(e.getDefaultMessage());
}
@ExceptionHandler(CustomException.class)
public R businessException(CustomException e) {
if (StringUtils.isNotBlank(e.getCode())) {
return R.error(e.getMessage());
}
return R.error("-1", e.getMessage());
}
@ExceptionHandler(Exception.class)
public R handleException(Exception e) {
log.error(e.getMessage(), e);
return R.error(String.format("未知错误%s",e.getMessage()));
}
}
其中状态码我们可以单独定义一个类,我为了方便就不写了。
5测试@RestController
public class TestAction {
@RequestMapping("/test")
public R test(){
return R.success("请求成功",null);
}
@RequestMapping("/test2")
public R test2(){
Student student=new Student();
student.setName("king");
student.setPassword("123456");
return R.success(student);
}
@RequestMapping("/test3")
public R test3(){
return R.error("请求失败");
}
@RequestMapping("/test4")
public R test4(){
throw new CustomException("失败了");
}
@RequestMapping("/test5")
public R test5() throws Exception {
throw new Exception("失败了");
}
}
大家可以自己去看看,如果有问题欢迎来骚扰我。



