Spring中ResponseBodyAdvice的使用详解

 更新时间:2021年10月29日 09:38:59   作者:七国的天下,我要九十九  
这篇文章主要介绍了Spring中ResponseBodyAdvice的使用,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

ResponseBodyAdvice可以在注解@ResponseBody将返回值处理成相应格式之前操作返回值。实现这个接口即可完成相应操作。可用于对response 数据的一些统一封装或者加密等操作

1 ResponseBodyAdvice的简介

ResponseBodyAdvice接口和之前记录的RequestBodyAdvice接口类似, RequestBodyAdvice是请求到Controller之前拦截,做相应的处理操作, 而ResponseBodyAdvice是对Controller返回的{@code @ResponseBody}or a {@code ResponseEntity} 后,{@code HttpMessageConverter} 类型转换之前拦截, 进行相应的处理操作后,再将结果返回给客户端.

ResponseBodyAdvice的源代码:

/**   数据的处理顺序向下
 * Allows customizing the response after the execution of an {@code @ResponseBody}
 * or a {@code ResponseEntity} controller method but before the body is written
 * with an {@code HttpMessageConverter}.
 *
 * <p>Implementations may be registered directly with
 * {@code RequestMappingHandlerAdapter} and {@code ExceptionHandlerExceptionResolver}
 * or more likely annotated with {@code @ControllerAdvice} in which case they
 * will be auto-detected by both.
 *
 * @author Rossen Stoyanchev
 * @since 4.1
 * @param <T> the body type
 */
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
	 * 返回 true 则下面 beforeBodyWrite方法被调用, 否则就不调用下述方法
	 */
	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);

}

说明:

  • 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) {

        // 遇到feign接口之类的请求, 不应该再次包装,应该直接返回
        // 上述问题的解决方案: 可以在feign拦截器中,给feign请求头中添加一个标识字段, 表示是feign请求
        // 在此处拦截到feign标识字段, 则直接放行 返回body.

        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;

    // {@code ResponseEntity} 案列
    @GetMapping("/one")
    @ResponseBody
    public User one() {

        List<User> users = userMapper.selectAll();
        System.out.println(users.get(0));
        return users.get(0);
    }

    
    // {@code @ResponseBody}  案列
    @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: 如果直接响应字符串返回,则会报类型转换异常.

到此这篇关于Spring中ResponseBodyAdvice的使用的文章就介绍到这了,更多相关Spring中ResponseBodyAdvice使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java 改造ayui表格组件实现多重排序

    Java 改造ayui表格组件实现多重排序

    layui 的表格组件目前只支持单列排序,在实际应用中并不能很好的支撑我们的业务需求。今天一时手痒,决定改造一番以支持多重排序。
    2021-04-04
  • java实现发牌小程序

    java实现发牌小程序

    这篇文章主要为大家详细介绍了java实现发牌小程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • spring中@RestController和@Controller的区别小结

    spring中@RestController和@Controller的区别小结

    @RestController和@Controller这两个注解用于创建Web应用程序的控制器类,那么这两个注解有哪些区别,本文就来介绍一下,并用示例代码说明,感兴趣的可以了解一下
    2023-09-09
  • MyBatis-Plus内置接口方法的具体使用

    MyBatis-Plus内置接口方法的具体使用

    java开发应用程序与数据库交互使用比较多的就是mybatisPlus接口,本文主要介绍了MyBatis-Plus内置接口方法的具体使用,具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12
  • 配合Swagger使用绝佳的两款直观易用JSON可视化工具

    配合Swagger使用绝佳的两款直观易用JSON可视化工具

    这篇文章主要为大家介绍了配合Swagger使用绝佳的两款直观易用JSON可视化工具图文详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • springboot使用spring-data-jpa操作MySQL数据库

    springboot使用spring-data-jpa操作MySQL数据库

    这篇文章主要介绍了springboot使用spring-data-jpa操作MySQL数据库,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • SpringBoot+SpringCache实现两级缓存(Redis+Caffeine)

    SpringBoot+SpringCache实现两级缓存(Redis+Caffeine)

    这篇文章主要介绍了SpringBoot+SpringCache实现两级缓存(Redis+Caffeine),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • SpringBoot中间件之封装统一白名单配置

    SpringBoot中间件之封装统一白名单配置

    这篇文章主要介绍了SpringBoot中间件封装统一白名单配置,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-07-07
  • java实现图片的上传与展示实例代码

    java实现图片的上传与展示实例代码

    这篇文章主要给大家介绍了关于java实现图片的上传与展示的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-12-12
  • 在SpringBoot中配置日期格式化的方法详解

    在SpringBoot中配置日期格式化的方法详解

    通常情况下,发起一个 Http 请求,Spring Boot 会根据请求路径映射到指定 Controller 上的某个方法的参数上,接着,Spring 会自动进行类型转换,对于日期类型的参数,Spring 默认是没有配置如何将字符串转换成日期类型的,本文将给大家介绍在SpringBoot中配置日期格式化的方法
    2023-10-10

最新评论