关于Spring中ResponseBodyAdvice的使用
ResponseBodyAdvice 可以在注解@ResponseBody 将返回值处理成相应格式之前操作返回值。实现这个接口即可完成相应操作。可用于对response 数据的一些统一封装或者加密等操作
1 ResponseBodyAdvice的简介
ResponseBodyAdvice接口和之前记录的RequestBodyAdvice接口类似, RequestBodyAdvice是请求到Controller之前拦截,做相应的处理操作, 而ResponseBodyAdvice是对Controller返回的{@code @ResponseBody}or a {@code ResponseEntity} 后,{@code HttpMessageConverter} 类型转换之前拦截, 进行相应的处理操作后,再将结果返回给客户端.
ResponseBodyAdvice的源代码:
public interface ResponseBodyAdvice<T> {
boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType);
@Nullable
T beforeBodyWrite(@Nullable T body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType,
ServerHttpRequest request, ServerHttpResponse response);
}
说明:
- supports方法: 判断是否要执行beforeBodyWrite方法,true为执行,false不执行. 通过该方法可以选择哪些类或那些方法的response要进行处理, 其他的不进行处理.
- beforeBodyWrite方法: 对response方法进行具体操作处理
{@code @ResponseBody} 返回响应体, 例如List集合
{@code ResponseEntity} 返回响应实体对象,例如User对象
2 ResponseBodyAdvice的使用
1 准备一个SpringBoot项目环境
2 添加一个响应拦截类
@ControllerAdvice
public class BaseResponseBodyAdvice implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
return true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType,
MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request,
ServerHttpResponse response) {
System.out.println("响应拦截成功");
if (body instanceof BaseResponse) {
return body;
} else if (body == null) {
return BaseResponse.ok();
} else {
return BaseResponse.ok(body);
}
}
}
3 添加一个返回包装类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BaseResponse<T> {
private T data;
private int status = 200;
private String message;
private long srvTime = System.currentTimeMillis();
public BaseResponse(String message) {
this.message = message;
}
public BaseResponse<T> setData(T data) {
this.data = data;
return this;
}
public static <T> BaseResponse<T> ok() {
return new BaseResponse<>("操作成功");
}
public static <T> BaseResponse<T> ok(T data) {
return new BaseResponse<T>("操作成功").setData(data);
}
}
4 添加控制类
@Controller
@RequestMapping("/hello")
public class HelloWorld {
@Autowired
private UserMapper userMapper;
@GetMapping("/one")
@ResponseBody
public User one() {
List<User> users = userMapper.selectAll();
System.out.println(users.get(0));
return users.get(0);
}
@GetMapping("/list")
@ResponseBody
public List<User> list() {
List<User> users = userMapper.selectAll();
System.out.println(users);
return users;
}
}
5 接口测试
浏览器访问: http://localhost:8080/hello/one
User(id=1, username=李子柒, phone=77777, icon=李子柒的头像, queryTime=Wed Oct 27 20:47:02 CST 2021)
响应拦截成功
浏览器访问: http://localhost:8080/hello/list
[User(id=1, username=李子柒, phone=77777, icon=李子柒的头像, queryTime=Wed Oct 27 20:46:58 CST 2021)]
响应拦截成功
ps: 如果直接响应字符串返回,则会报类型转换异常.
|