当前位置:网站首页>使用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));
}
}
测试结果


边栏推荐
- MySQL deep paging optimization with tens of millions of data, and online failure is rejected!
- 事项研发工作流全面优化|Erda 2.2 版本如“七”而至
- 有些事情让感情无处安放
- 123456
- Pytoch practice -- MNIST dataset handwritten digit recognition
- Teach yourself to train pytorch model to Caffe (III)
- [case] Application of positioning - Taobao rotation map
- PVC 塑料片BS 476-6 火焰传播性能测定
- Écrire une interface basée sur flask
- Objects in the list, sorted by a field
猜你喜欢

Pytoch practice -- MNIST dataset handwritten digit recognition

What should I do to prepare for the interview algorithm position during school recruitment?

木板ISO 5660-1 热量释放速率摸底测试

Teach yourself to train pytorch model to Caffe (III)

Golang (1) | from environmental preparation to quick start

EN 438-7 laminated sheet products for building covering decoration - CE certification

張麗俊:穿透不確定性要靠四個“不變”

Clion configures Visual Studio (MSVC) and JOM multi-core compilation

Explain various hot issues of Technology (SLB, redis, mysql, Kafka, Clickhouse) in detail from the architecture

Why can't Chinese software companies produce products? Abandon the Internet after 00; Open source high-performance API gateway component of station B | weekly email exclusive to VIP members of Menon w
随机推荐
Écrire une interface basée sur flask
【日常训练--腾讯精选50】89. 格雷编码(看题解才会的)
100 cases of shell programming
R language [data management]
股票开户选择哪家证券公司比较好哪家平台更安全
使用WebAssembly在浏览器端操作Excel
vant 源码解析之 utils/index.ts 工具函数
PVC 塑料片BS 476-6 火焰传播性能测定
selenium 获取dom内验证码图片
Some things make feelings nowhere to put
What are the requirements of UL 2043 test for drive housing in the United States?
基於flask寫一個接口
uni-app 蓝牙通信
Sequence alignment
Display DIN 4102-1 Class B1 fire test requirements
Selenium gets the verification code image in DOM
PostGIS installation geographic information extension
Prior knowledge of machine learning in probability theory (Part 1)
Haas506 2.0 development tutorial - Alibaba cloud OTA - PAC firmware upgrade (only supports versions above 2.2)
Deep merge object deep copy of vant source code parsing