Spring的 @ControllerAdvice 之 ResponseBodyAdvice对响应结果进行增强

Spring的 @ControllerAdvice 之 ResponseBodyAdvice对响应结果进行增强

1. 使用背景

对响应结果进行统一结果处理时,有时会出现有的接口未进行封装,为了解决该问题,可使用@ControllerAdvice 注解对响应结果进行aop编程增强。

2. 使用方法

package com.banneroa.utils;

import cn.hutool.core.annotation.AnnotationUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * @author rjj
 * @date 2023/10/21 - 8:58
 */
@ControllerAdvice
public class ResponseResultHandler implements ResponseBodyAdvice<Object> {

    //对符合条件的响应进行处理
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        //查看是否有@ResponseBody注解,存在才进行处理
        if (returnType.getMethodAnnotation(ResponseBody.class) != null
                || AnnotationUtils.findAnnotation(returnType.getContainingClass(), ResponseBody.class) != null) {
            return true;
        }
        return false;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {

        if (body instanceof ResponseResult){
            //如果是统一结果类型不处理
            return body;
        }
        //不是统一类型,进行封装
        return ResponseResult.okResult(body);
    }
}

3. 结果

接口示例:
在这里插入图片描述
使用@ControllerAdvice 之前:
在这里插入图片描述
使用之后:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,很高兴能回答您的问题。 首先,让我们了解一下 @ControllerAdvice 注解。它是 Spring MVC 框架提供的一种全局异常处理机制,可以让我们在一个地方集中处理所有 Controller 抛出的异常。 使用 @ControllerAdvice 注解的类可以包含多个 @ExceptionHandler 注解的方法,每个方法处理不同的异常类型。当 Controller 抛出异常时,Spring 会根据异常类型找到对应的 @ExceptionHandler 方法进行处理。 以下是一个简单的 @ControllerAdvice 示例: ```java @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity<String> handleException(Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Something went wrong!"); } @ExceptionHandler(MyCustomException.class) public ResponseEntity<String> handleMyCustomException(MyCustomException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); } } ``` 在上面的例子中,我们定义了两个 @ExceptionHandler 方法,分别处理 Exception 和 MyCustomException 类型的异常。 如果您想要更细粒度地处理异常,可以在 @ExceptionHandler 注解中指定具体的异常类型,如下所示: ```java @ExceptionHandler({IOException.class, SQLException.class}) public void handleIOExceptionOrSQLException() { // ... } ``` 另外,@ControllerAdvice 还支持自定义返回值类型和异常解析器,具体可以参考 Spring 官方文档。 最后,关于统一处理异常,除了使用 @ControllerAdvice,我们还可以使用 Spring Boot 提供的 ErrorController 接口或实现自己的异常处理器。 希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值