spring boot 异常处理,响应Response重写AOP实现

异常处理注解形式

import com.example.exception.demo.common.WebMessage;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description: 异常捕获处理
 * @Author: Tan
 * @Date: 2019/12/24
 **/
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 异常处理
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object exceptionHandler(Exception e){
        e.printStackTrace();
        return WebMessage.fail(e.getMessage());
    }
}

截获响应实体

import com.example.exception.demo.utils.JsonUtils;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * @Description: TODO
 * @Author: Tan
 * @Date: 2019/12/24
 **/
@ControllerAdvice
public class ResponseBodyAnalysis implements ResponseBodyAdvice {

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter arg1,
                                  MediaType arg2, Class arg3, ServerHttpRequest arg4,
                                  ServerHttpResponse arg5) {
        System.out.println(JsonUtils.objectToJson(body));
        return body;
    }

    @Override
    public boolean supports(MethodParameter arg0, Class arg1) {
        return true;
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
是的,Spring Boot可以基于AOP实现指定切面的拦截处理。AOP(面向切面编程)是一种编程范式,可以将与业务逻辑无关的横切关注点(如日志记录、安全控制、性能统计等)通过切面的方式进行集中管理。 在Spring Boot中,可以通过在切面类上使用`@Aspect`注解来标识一个类为切面类,通过在切面类的方法上使用不同的注解(如`@Before`、`@After`、`@Around`等)来指定不同类型的切点拦截处理。 例如,我们可以定义一个切面类`LogAspect`,用于记录所有控制层方法的执行时间和返回结果: ```java @Aspect @Component public class LogAspect { private static final Logger logger = LoggerFactory.getLogger(LogAspect.class); @Around("execution(* com.example.myapp.controller..*.*(..))") public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long startTime = System.currentTimeMillis(); Object result = joinPoint.proceed(); long endTime = System.currentTimeMillis(); long executionTime = endTime - startTime; logger.info("{} executed in {} ms, returned: {}", joinPoint.getSignature(), executionTime, result); return result; } } ``` 在上述代码中,`@Aspect`注解标识了`LogAspect`类为切面类,`@Around`注解指定了要拦截的切点为`com.example.myapp.controller`包下的所有方法,`logExecutionTime()`方法用于记录方法的执行时间和返回结果,并将日志输出到控制台中。 当控制层的方法被调用时,如果方法的签名匹配到了切点表达式,那么`LogAspect`类中对应的切面方法(如`logExecutionTime()`)就会被执行,从而实现了对方法的拦截处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

cy谭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值