当前位置:网站首页>使用Aspect制作全局异常处理类
使用Aspect制作全局异常处理类
2022-07-05 21:24:00 【哎呦喂O_o嗨】
新增一个注解类
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));
}
}
测试结果
边栏推荐
- Clion configures Visual Studio (MSVC) and JOM multi-core compilation
- Determine the best implementation of horizontal and vertical screens
- Mode - "Richter replacement principle"
- Get JS of the previous day (timestamp conversion)
- How to prepare for the algorithm interview and answer the algorithm interview questions
- Two ways to realize video recording based on avfoundation
- [case] Application of element display and hiding -- element mask
- 校招期间 准备面试算法岗位 该怎么做?
- vant 源码解析 event.ts 事件处理 全局函数 addEventListener详解
- Comprehensive optimization of event R & D workflow | Erda version 2.2 comes as "7"
猜你喜欢
R language [data management]
阿里云有奖体验:用PolarDB-X搭建一个高可用系统
Teach yourself to train pytorch model to Caffe (III)
How to prepare for the algorithm interview and answer the algorithm interview questions
Golang(1)|从环境准备到快速上手
Arcgis\qgis no plug-in loading (no offset) mapbox HD image map
Pytoch practice -- MNIST dataset handwritten digit recognition
【案例】定位的运用-淘宝轮播图
面试官:并发编程实战会吗?(线程控制操作详解)
Simple interest mode - evil Chinese style
随机推荐
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
Zhang Lijun: penetrating uncertainty depends on four "invariants"
Mode - "Richter replacement principle"
EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
Golang(1)|从环境准备到快速上手
2022-07-03-CKA-粉丝反馈最新情况
uni-app 蓝牙通信
Uni app Bluetooth communication
MySQL InnoDB Architecture Principle
Display DIN 4102-1 Class B1 fire test requirements
阿里云有奖体验:用PolarDB-X搭建一个高可用系统
[case] Application of element display and hiding -- element mask
@Validated基础参数校验、分组参数验证和嵌套参数验证
Pytoch practice -- MNIST dataset handwritten digit recognition
Postgres establish connection and delete records
When a user logs in, there is often a real-time drop-down box. For example, entering an email will @qq com,@163. com,@sohu. com
木板ISO 5660-1 热量释放速率摸底测试
LeetCode: Distinct Subsequences [115]
Zhang Lijun: la pénétration de l’incertitude dépend de quatre « invariants»
How to send samples when applying for BS 476-7 display? Is it the same as the display??