当前位置:网站首页>Making global exception handling classes with aspect
Making global exception handling classes with aspect
2022-07-05 21:26:00 【Ouch, hello o_ O hi】
Add an annotation class
package com.gzzh.web.core.exceptionHandler;
import java.lang.annotation.*;
/** * @Description: Custom annotation handling exception * @Author yizhixiansheng * @Date 2022/7/1 */
@Documented
@Target({
ElementType.METHOD, ElementType.TYPE}) // It can be used in classes or methods
@Retention(RetentionPolicy.RUNTIME) // Runtime exception
public @interface CustomExceptionCatch {
}
New global exception handling component
This component comes with spring Created and scanned at startup The above notes The identified class or method
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: Custom exception handling class * @Author yizhixiansheng * @Date 2022/7/1 */
@Component
@Aspect
@Slf4j
public class CustomExceptionHandler {
/** * Custom global exception handling class * @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(" Failed to process data ");
// Get the target class name
String clazzName = joinPoint.getTarget().getClass().getName();
// Get the target class method name
String methodName = joinPoint.getSignature().getName();
long start = System.currentTimeMillis();
try {
// Calling the target method
result = joinPoint.proceed();
long time = System.currentTimeMillis() - start;
log.info( "{}: {}: Call data consumption : {} ms", clazzName, methodName, time);
}catch (Throwable e){
log.error( "{}: {}: Data processing failed :{}", clazzName, methodName,e);
}
return result;
}
}
Use
@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;
/** * Get the comprehensive evaluation description * @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());
}
/** * Get a comprehensive evaluation * @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));
}
}
test result
边栏推荐
- Zhang Lijun: penetrating uncertainty depends on four "invariants"
- Aitm 2-0003 horizontal combustion test
- systemd-resolved 开启 debug 日志
- Which securities company is better and which platform is safer for stock account opening
- MySQL deep paging optimization with tens of millions of data, and online failure is rejected!
- selenium 获取dom内属性值的方法
- Simple interest mode - evil Chinese style
- Comprehensive optimization of event R & D workflow | Erda version 2.2 comes as "7"
- sql常用语法记录
- Teach yourself to train pytorch model to Caffe (III)
猜你喜欢
Introduction of ArcGIS grid resampling method
示波器探头对测量带宽的影响
Cross end solution to improve development efficiency rapidly
How to send samples when applying for BS 476-7 display? Is it the same as the display??
ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图
PVC plastic sheets BS 476-6 determination of flame propagation properties
Longest swing sequence [greedy practice]
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
ArcGIS栅格重采样方法介绍
Teach yourself to train pytorch model to Caffe (III)
随机推荐
木板ISO 5660-1 热量释放速率摸底测试
wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
Realize the function of verifying whether the user has completed login when browsing the page
Deep merge object deep copy of vant source code parsing
Using webassembly to operate excel on the browser side
基于 Ingress Controller 在集群外访问 Zadig 自测环境(最佳实践)
CLion配置visual studio(msvc)和JOM多核编译
Chapter 05_ Storage engine
大二下个人发展小结
AITM2-0002 12s或60s垂直燃烧试验
办公遇到的问题--
Sequence alignment
Selenium's method of getting attribute values in DOM
冯唐“春风十里不如你”数字藏品,7月8日登录希壤!
让开发效率飞速提升的跨端方案
Selenium gets the verification code image in DOM
浅聊我和一些编程语言的缘分
Careercup its 1.8 serial shift includes problems
阿里云有奖体验:用PolarDB-X搭建一个高可用系统
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session