ResponseBodyAdvice 對伺服器返回值進行封裝
阿新 • • 發佈:2018-12-10
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; }