1. 为什么这么做
前端方便处理 后端返回的数据多种多样 不统一的话前端会杀人
2. 实操
我是结合了我看到的所有方案搞得一套 全自动收集异常以及自动包装结果无需在controller中调用的模式,如果需要自定义更改或删除相应文件即可
exception包下(目类结构也可以自行调整)
(1)创建异常枚举类(用来以后统一管理业务异常)
package org.example.springtest.exception;
import lombok.Getter;
@Getter
public enum AppExceptionCodeMsg {
INVALID_CODE(10000,"验证码无效"),
USERNAME_NOT_EXITS(10001,"用户名不存在"),
;
private int code;
private String msg;
AppExceptionCodeMsg(int code,String msg){
this.code=code;
this.msg=msg;
}
}
(2)创建异常包装类
package org.example.springtest.exception;
import lombok.Getter;
import org.example.springtest.exception.AppExceptionCodeMsg;
@Getter
public class AppException extends RuntimeException{
private int code=500;
private String msg="服务器异常";
public AppException(AppExceptionCodeMsg appExceptionCodeMsg){
super();
this.code=appExceptionCodeMsg.getCode();
this.msg=appExceptionCodeMsg.getMsg();
}
public AppException(int code,String msg){
super();
this.code=code;
this.msg=msg;
}
}
modal包下
(3)包装返回结果类
package org.example.springtest.modal;
import lombok.Getter;
import org.example.springtest.exception.AppExceptionCodeMsg;
@Getter
public class Resp<T> {
private int code=200;
private String msg="success";
private final T data;
private boolean success=true;
private Resp(int code,String msg,T data,boolean success){
this.code=code;
this.msg=msg;
this.data=data;
this.success=success;
}
public static <T> Resp success(T data){
return new Resp(200,"success",data,true);
}
public static <T> Resp success(String msg,T data){
return new Resp(200,msg,data,true);
}
public static <T>Resp error(int code,String msg){
return new Resp(code,msg,null,false);
}
public static <T>Resp error(AppExceptionCodeMsg appExceptionCodeMsg){
return new Resp(appExceptionCodeMsg.getCode(),appExceptionCodeMsg.getMsg(),null,false);
}
}
config下
(4)第一部分是异常自动捕获自动包装
package org.example.springtest.config;
import org.example.springtest.exception.AppException;
import org.example.springtest.modal.Resp;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = {Exception.class})
@ResponseBody
public <T> Resp<T> exceptionHandler(Exception e) {
if (e instanceof AppException) {
AppException appException=(AppException)e;
return Resp.error(appException.getCode(),appException.getMsg());
}
return Resp.error(500,"服务器异常");
}
}
(5)第二部分是成功的结果自动捕获自动包装
package org.example.springtest.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.example.springtest.modal.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
@RestControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice<Object> {
@Autowired
private ObjectMapper objectMapper;
@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
return true;
}
@SneakyThrows
@Override
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
if(o instanceof String){
return objectMapper.writeValueAsString(Resp.success(o));
}
if(o instanceof Resp){
return o;
}
return Resp.success(o);
}
}
ok然后就大功告成了 上面的逻辑都比较简单也没上面多讲的 直接看效果
例子:可以看到我在controller类里是完全不调用我们的包装的
package org.example.springtest.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String Hello(){
return "Hello Word!";
}
}
返回结果
我们也可以抛出异常尝试一下
@Operation(summary = "异常测试")
@GetMapping("/exceptionTest")
public String exceptionTest(){
throw new AppException(AppExceptionCodeMsg.INVALID_CODE);
}
结果