1.实现接口中的方法
*
* @author Rossen Stoyanchev
* @since 4.1
*/
public interface ResponseBodyAdvice<T> {
/**
* Whether this component supports the given controller method return type
* and the selected {@code HttpMessageConverter} type.
* @param returnType the return type
* @param converterType the selected converter type
* @return {@code true} if {@link #beforeBodyWrite} should be invoked;
* {@code false} otherwise
*/
boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType);
/**
* Invoked after an {@code HttpMessageConverter} is selected and just before
* its write method is invoked.
* @param body the body to be written
* @param returnType the return type of the controller method
* @param selectedContentType the content type selected through content negotiation
* @param selectedConverterType the converter type selected to write to the response
* @param request the current request
* @param response the current response
* @return the body that was passed in or a modified (possibly new) instance
*/
@Nullable
T beforeBodyWrite(@Nullable T body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType,
ServerHttpRequest request, ServerHttpResponse response);
}
2.方法讲解
supports对你需要进行拦截的response进行判断筛选,返回true则进行拦截反之放行,
beforeBodyWrite对supports进行拦截的response进行处理,封装你需要的类型参数,加密等等。
private static Class<?>[] responseType = { Collection.class, String.class, Integer.class, Date.class };
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
if (!returnType.getContainingClass().getPackage().getName().startsWith(ProjectConstant.API_PACKAGE))
return false;
for (int i = 0; i < responseType.length; i++) {
if (responseType[i].isAssignableFrom(returnType.getMethod().getReturnType())
|| returnType.getMethod().getReturnType().isArray()) {
return true;
}
}
return false;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
ServerHttpResponse response) {
if (Collection.class.isAssignableFrom(returnType.getMethod().getReturnType())
|| returnType.getMethod().getReturnType().isArray()) {
// 数组
if (MediaType.TEXT_HTML.equals(selectedContentType) || MediaType.TEXT_PLAIN.equals(selectedContentType))
if (!Stream.of(returnType.getMethod().getAnnotations()).anyMatch(a -> {
if (a instanceof RequestMapping) {
return !Objs.isEmpty(((RequestMapping) a).produces());
}
return false;
})) {
response.getHeaders()
.setContentType(MediaType.parseMediaType(MediaType.APPLICATION_JSON_UTF8_VALUE));
return JSONObject.toJSONString(Pages.builder((Collection<?>) body));
}
return Pages.builder((Collection<?>) body);
}
JSONObject js = new JSONObject();
js.put(AppEnum.status.getValue(), true);
js.put(AppEnum.message.getValue(), body);
if (MediaType.TEXT_HTML.equals(selectedContentType) || MediaType.TEXT_PLAIN.equals(selectedContentType)) {
if (!Stream.of(returnType.getMethod().getAnnotations()).anyMatch(a -> {
if (a instanceof RequestMapping) {
return !Objs.isEmpty(((RequestMapping) a).produces());
}
return false;
}))
response.getHeaders().setContentType(MediaType.parseMediaType(MediaType.APPLICATION_JSON_UTF8_VALUE));
return js.toString();
}
return js;
}