参考Spring Boot @RestControllerAdvice 统一异常处理、@RestControllerAdvice构造统一返回值格式和统一异常处理、SpringBoot 使用 beforeBodyWrite 实现统一的接口返回类型
1.创建好我们的统一返回格式
//这些是lombok的注解
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResponse<T> {
private boolean success;
private int code;
private String message;
private T data;
public CommonResponse(Integer code, String message, Boolean success) {
this.code = code;
this.message = message;
this.success = success;
}
}
2. 实现RestControllerAdvice,完成对返回信息的捕获
@RestControllerAdvice
public class CommonResponseDataAdvice implements ResponseBodyAdvice {
@Override
public boolean supports(MethodParameter methodParameter, Class aClass) {
//通过方法参数得到类名, 然后得到类上的annotation,
// 如果被IgnoreResponseAdvice标识就不拦截
if (me