0 课程地址
https://coding.imooc.com/lesson/380.html#mid=28593
1 浓缩精华
1.1 4.1
2 个人关注
2.1 注解类的编写
2.2 @RestControllerAdvice 注解理解
@RestControllerAdvice源码可以知道,它就是@ControllerAdvice和@ResponseBody的合并。此注解全局数据预处理。
2.3 如何判断某个类或者方法是否添加某个注解?
if(methodParameter.getDeclaringClass().isAnnotationPresent(IgnoreResponseAdvice.class) || methodParameter.getMethod().isAnnotationPresent(IgnoreResponseAdvice.class)){
2.4 判断对象是否属于某个类?
}else if(o instanceof CommonResponse){
3 课程内容
3.1 统一响应流程
客户端发起httpRequest请求到后端服务中(后端controller调用service再调用dao),最终返回httpResponse,返回json或view.本章只返回json
3.2 统一响应定义
统一响应就是将原来的相应做了一层固定格式的封装
4 代码演练
4.1 统一响应代码编写
统一响应主类
package com.imooc.coupon.vo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; /** * CommonResponse * * @author 魏豆豆 * @date 2021/8/3 */ @Data//省略getset方法编写 @NoArgsConstructor//无参构造 @AllArgsConstructor//有参构造 public class CommonResponse<T> implements Serializable { private String code; private String describe; private T data; public CommonResponse(String code,String describe){ this.code = code; this.describe = describe; } }
忽略统一响应注解类(针对不想使用统一响应的情况)
package com.imooc.coupon.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 忽略统一响应定义 */ @Target({ElementType.TYPE,ElementType.METHOD})//当前注解可以定义到类或者方法上 @Retention(RetentionPolicy.RUNTIME)//在何时起作用?运行时起作用 public @interface IgnoreResponseAdvice { }
通知增强类
package com.imooc.coupon.advice; import com.imooc.coupon.annotation.IgnoreResponseAdvice; import com.imooc.coupon.vo.CommonResponse; 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; /** * 该方法是是否执行注解,如果类上有IgnoreResponseAdvice注解或者方法上有IgnoreResponseAdvice注解,则不执行统一响应代码,否则,则需要执行 * * @author 魏豆豆 * @date 2021/8/4 */ @RestControllerAdvice public class CommonResponseDataAdvice implements ResponseBodyAdvice<Object> { @Override @SuppressWarnings("all") public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) { if(methodParameter.getDeclaringClass().isAnnotationPresent(IgnoreResponseAdvice.class) || methodParameter.getMethod().isAnnotationPresent(IgnoreResponseAdvice.class)){ return false; } return true; } /** * 该方法是执行统一响应的方法,如果无返回值(返回值是response),本身是CommonResponse类,则不用转化为统一响应格式, * 否则,需要转化 * @param methodParameter * @param aClass * @return */ @Override @SuppressWarnings("all") public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { CommonResponse<Object> response = new CommonResponse<>(0,""); if(null == o){ return response; }else if(o instanceof CommonResponse){ response = (CommonResponse<Object>) o; }else{ response.setData(o); } return response; } }
诸葛