springboot项目用ResponseBodyAdvice统一包装返回体-笔记

文章介绍了如何在SpringBoot项目中利用ResponseBodyAdvice接口来统一包装RESTAPI的返回数据,这样可以简化后端工作,方便数据结构的变更。通过定义一个实现了ResponseBodyAdvice接口的类,并在Controller中应用,可以自动对所有返回体进行增强处理,确保返回的数据结构一致,同时也便于处理异常和日志记录。

springboot项目用ResponseBodyAdvice统一包装返回体-笔记

在使用springboot开发rest api接口时,往往需要和前端对接定义返回的数据结构,这时使用ResponseBodyAdvice统一包装返回体给前端,可以大大减轻后端的工作量,同时也方便对返回体的数据结构做灵活变更。
使用ResponseBodyAdvice封装好统一的返回体后,Controller中就可以专心处理业务模型和业务数据了。

定义1个ResponseBodyAdvice接口的实现类

/**
 * 实现 ResponseBodyAdvice接口,并重写其中的方法,即可对我们的controller进行增强操作
 *
 * 不管Controller中 是否使用ResultVO<T>对返回结果包装,
 * 均可被 ResponseBodyAdvice接口 处理返回数据,以使系统 统一结果,统一异常,统一日志
 */
// 注意哦,这里要加上需要扫描的包
@RestControllerAdvice(basePackages = {
   
   "cn.ath.knowwikibackend.rest"})
public class SysRestBodyAdvice implements ResponseBodyAdvice<Object> {
   
   
    /**
     * Whether this component supports the given controller method return type
     * and the selected {@code HttpMessageConverter} type.
     *
     * supports方法要返回为true才会执行beforeBodyWrite方法,
     * 所以如果有些情况不需要进行增强操作可以在supports方法里进行判断
     *
     * @param returnType    the return type
     * @param converterType the selected converter type
     * @return {@code true} if {@link #beforeBodyWrite} should be invoked;
     * {@code false} otherwise
     */
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
   
   
        // 如果接口返回的类型本身就是ResultVO那就没有必要进行额外的操作,返回false
        return !returnType.getGenericParameterType().equals(ResultVO.class);
    }

    /**
     * Invoked after an {@code HttpMessageConverter} is selected and just before
     * its write method is invoked.
     *
     * 对返回数据进行真正的操作还是在beforeBodyWrite方法中,
     * 我们可以直接在该方法里包装数据,这样就不需要每个接口都进行数据包装了,省去了很多麻烦
     *
     * @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
     */
    @SneakyThrows
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
   
   

        // String类型不能直接包装,所以要进行些特别的处理
        if (returnType.getGenericParameterType().equals(String.class)) {
   
   
            ObjectMapper
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ThinkPet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值