使用Aspect制作全局异常处理类

新增一个注解类

package com.gzzh.web.core.exceptionHandler;

import java.lang.annotation.*;

/**
 * @Description: 自定义注解处理异常
 * @Author yizhixiansheng
 * @Date 2022/7/1
 */
@Documented
@Target({ElementType.METHOD, ElementType.TYPE}) //可在类或者方法使用
@Retention(RetentionPolicy.RUNTIME) // 运行时异常
public @interface CustomExceptionCatch {
}

新增全局异常处理组件

该组件随spring启动时创建并扫描被 上述注解 所标识的类或方法

package com.gzzh.web.core.exceptionHandler;

import com.gzzh.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @Description: 自定义异常处理类
 * @Author yizhixiansheng
 * @Date 2022/7/1
 */
@Component
@Aspect
@Slf4j
public class CustomExceptionHandler {

    /**
     * 自定义全局异常处理类
     * @param joinPoint
     * @return java.lang.Object
     * @author yizhixiansheng
     * @create 2022/7/1
     **/
    @Around("@annotation(com.gzzh.web.core.exceptionHandler.CustomExceptionCatch) || @within(com.gzzh.web.core.exceptionHandler.CustomExceptionCatch)")
    public Object around(ProceedingJoinPoint joinPoint) {
        Object result = AjaxResult.error("处理数据失败");
        // 获取目标类名称
        String clazzName = joinPoint.getTarget().getClass().getName();
        // 获取目标类方法名称
        String methodName = joinPoint.getSignature().getName();
        long start = System.currentTimeMillis();
        try {
            // 调用目标方法
            result = joinPoint.proceed();
            long time = System.currentTimeMillis() - start;
            log.info( "{}: {}: 调用数据耗费: {} ms", clazzName, methodName, time);
        }catch (Throwable e){
            log.error( "{}: {}: 数据处理失败:{}", clazzName, methodName,e);
        }
        return result;
    }
}

使用

@CustomExceptionCatch

package com.gzzh.web.controller.homePage;

import com.gzzh.common.core.domain.AjaxResult;
import com.gzzh.datafield.homePage.service.IHomePageService;
import com.gzzh.web.core.exceptionHandler.CustomExceptionCatch;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@Slf4j
@RestController
@RequestMapping("/homePage")
public class HomePageController {

    @Resource
    private IHomePageService homePageService;

    /**
     * 获取综合评价描述
     * @return com.gzzh.common.core.domain.AjaxResult
     * @author yizhixiansheng
     * @create 2022/6/18
     **/
    @CustomExceptionCatch
    @GetMapping("/getAppraiseDesc")
    public AjaxResult getAppraiseDesc() throws Exception{
        return AjaxResult.success(homePageService.getAppraiseDesc());
    }
    
    /**
     * 获取综合评价
     * @return com.gzzh.common.core.domain.AjaxResult
     * @author yizhixiansheng
     * @create 2022/6/18
     **/
    @CustomExceptionCatch
    @GetMapping("/getAppraise/{latnId}")
    public AjaxResult getAppraise(@PathVariable Long latnId) throws Exception {
        return AjaxResult.success(tbCwlcMarperService.getAppraise(latnId));
    }
}

测试结果

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

哎呦喂O_o嗨

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

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

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

打赏作者

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

抵扣说明:

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

余额充值