当前位置:网站首页>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
边栏推荐
- LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line
- wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
- EasyExcel的讀寫操作
- 第05章_存储引擎
- selenium 获取dom内验证码图片
- [case] Application of element display and hiding -- element mask
- Realize the function of verifying whether the user has completed login when browsing the page
- 学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
- Get JS of the previous day (timestamp conversion)
- 显示屏DIN 4102-1 Class B1防火测试要求
猜你喜欢
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
请查收.NET MAUI 的最新学习资源
leetcode:1139. The largest square bounded by 1
Longest swing sequence [greedy practice]
leetcode:1755. Sum of subsequences closest to the target value
Comprehensive optimization of event R & D workflow | Erda version 2.2 comes as "7"
Realize the function of verifying whether the user has completed login when browsing the page
秋招将临 如何准备算法面试、回答算法面试题
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
Teach yourself to train pytorch model to Caffe (2)
随机推荐
2022-07-03-CKA-粉丝反馈最新情况
MySQL ifnull usage function
校招期间 准备面试算法岗位 该怎么做?
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
[case] Application of element display and hiding -- element mask
Longest swing sequence [greedy practice]
systemd-resolved 开启 debug 日志
"Grain mall" -- Summary and induction
Simple interest mode - evil Chinese style
Problems encountered in office--
Comparison table of foreign lead American abbreviations
使用WebAssembly在浏览器端操作Excel
Teach yourself to train pytorch model to Caffe (I)
Binary search
树莓派4B上ncnn转换出来的模型调用时总是崩溃(Segment Fault)的原因
Enclosed please find. Net Maui's latest learning resources
Golang (1) | from environmental preparation to quick start
R language [data management]
Display DIN 4102-1 Class B1 fire test requirements
@Validated basic parameter verification, grouping parameter verification and nested parameter verification