当前位置:   article > 正文

@ControllerAdvice,ResponseBodyAdvice 统一处理返回值/响应体_controlleradvice更改response

controlleradvice更改response

ResponseBodyAdvice是spring4.1的新特性,其作用是在响应体写出之前做一些处理;比如,修改返回值、加密等。

我在项目中的用到@ControllerAdvice,ResponseBodyAdvice的目的,是为每个请求的返回json中修改一个属性值。

ResponseBodyAdvice 接口源码:

  1. package org.springframework.web.servlet.mvc.method.annotation;
  2. import org.springframework.core.MethodParameter;
  3. import org.springframework.http.MediaType;
  4. import org.springframework.http.converter.HttpMessageConverter;
  5. import org.springframework.http.server.ServerHttpRequest;
  6. import org.springframework.http.server.ServerHttpResponse;
  7. /**
  8. * Allows customizing the response after the execution of an {@code @ResponseBody}
  9. * or an {@code ResponseEntity} controller method but before the body is written
  10. * with an {@code HttpMessageConverter}.
  11. Implementations may be may be registered directly with
  12. * {@code RequestMappingHandlerAdapter} and {@code ExceptionHandlerExceptionResolver}
  13. * or more likely annotated with {@code @ControllerAdvice} in which case they
  14. * will be auto-detected by both.
  15. *
  16. * @author Rossen Stoyanchev
  17. * @since 4.1
  18. */
  19. public interface ResponseBodyAdvice {
  20. /**
  21. * Whether this component supports the given controller method return type
  22. * and the selected {@code HttpMessageConverter} type.
  23. * @param returnType the return type
  24. * @param converterType the selected converter type
  25. * @return {@code true} if {@link #beforeBodyWrite} should be invoked, {@code false} otherwise
  26. */
  27. boolean supports(MethodParameter returnType, Class> converterType);
  28. /**
  29. * Invoked after an {@code HttpMessageConverter} is selected and just before
  30. * its write method is invoked.
  31. * @param body the body to be written
  32. * @param returnType the return type of the controller method
  33. * @param selectedContentType the content type selected through content negotiation
  34. * @param selectedConverterType the converter type selected to write to the response
  35. * @param request the current request
  36. * @param response the current response
  37. * @return the body that was passed in or a modified, possibly new instance
  38. */
  39. T beforeBodyWrite(T body, MethodParameter returnType, MediaType selectedContentType,
  40. Class> selectedConverterType,
  41. ServerHttpRequest request, ServerHttpResponse response);
  42. }

实现的示例:

  1. package com.xxx;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import javax.servlet.http.HttpServletRequest;
  6. import org.springframework.core.MethodParameter;
  7. import org.springframework.http.MediaType;
  8. import org.springframework.http.server.ServerHttpRequest;
  9. import org.springframework.http.server.ServerHttpResponse;
  10. import org.springframework.http.server.ServletServerHttpRequest;
  11. import org.springframework.web.bind.annotation.ControllerAdvice;
  12. import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
  13. import com.xxx.Messages;
  14. /**
  15. * 处理 返回值
  16. * @Description:TODO
  17. * @author lh
  18. * @time:2015-8-31 上午11:24:08
  19. */
  20. @ControllerAdvice
  21. public class MyResponseBodyAdvice implements ResponseBodyAdvice{
  22. @Override
  23. public Object beforeBodyWrite(Object returnValue, MethodParameter methodParameter,
  24. MediaType mediaType, Class clas, ServerHttpRequest serverHttpRequest,
  25. ServerHttpResponse serverHttpResponse) {
  26. //通过 ServerHttpRequest的实现类ServletServerHttpRequest 获得HttpServletRequest
  27. //ServletServerHttpRequest sshr=(ServletServerHttpRequest) serverHttpRequest;
  28. //此处获取到request 是为了取到在拦截器里面设置的一个对象 是我项目需要,可以忽略
  29. //HttpServletRequest request= sshr.getServletRequest();
  30. //将返回值returnValue转成我需要的类型Message<?> 方便统一修改其中的某个属性
  31. // Messages是我自定义的一个类
  32. Messages<?> msg=(Messages<?>) returnValue;
  33. //统一修改返回值/响应体
  34. msg.setXXX("测试修改返回值");
  35. //返回修改后的值
  36. return msg;
  37. }
  38. @Override
  39. public boolean supports(MethodParameter methodParameter, Class clas) {
  40. //获取当前处理请求的controller的方法
  41. String methodName=methodParameter.getMethod().getName();
  42. // 不拦截/不需要处理返回值 的方法
  43. String method= "loginCheck"; //如登录
  44. //不拦截
  45. return !method.equals(methodName);
  46. }
  47. }

这样得到的处理过的返回值,属性xxx的值都是“测试修改返回值”;

 

实践:

本需求解决历史遗留问题, 因为前后端沟通有误,导致前端使用code判断成功失败,而code成功后不做处理默认返回0,而规范是200,为了前端代码调用之前接口不进行修改,

所以使用@ControllerAdvice完成特定接口的拦截,除此之外判定成功统一重新为code赋值200

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/184001
推荐阅读
相关标签
  

闽ICP备14008679号