当前位置:网站首页>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


边栏推荐
猜你喜欢

阿里云有奖体验:用PolarDB-X搭建一个高可用系统

ArcGIS栅格重采样方法介绍

EasyExcel的讀寫操作

LeetCode_哈希表_困难_149. 直线上最多的点数

ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图

Feng Tang's "spring breeze is not as good as you" digital collection, logged into xirang on July 8!

Pytoch practice -- MNIST dataset handwritten digit recognition

Zhang Lijun: penetrating uncertainty depends on four "invariants"

示波器探头对测量带宽的影响

Clickhouse copy paste multi line SQL statement error
随机推荐
Golang(1)|从环境准备到快速上手
one hundred and twenty-three thousand four hundred and fifty-six
Objects in the list, sorted by a field
MySQL ifnull usage function
请查收.NET MAUI 的最新学习资源
字典树简单入门题(居然是蓝题?)
Selenium finds the contents of B or P Tags
Sophomore personal development summary
显示屏DIN 4102-1 Class B1防火测试要求
int GetMonth( ) const throw( );后面的throw( )什么意思?
ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图
面试官:并发编程实战会吗?(线程控制操作详解)
MySQL 千万数据量深分页优化, 拒绝线上故障!
leetcode:1755. Sum of subsequences closest to the target value
【案例】定位的运用-淘宝轮播图
Introduction to TS, constructor and its this, inheritance, abstract class and interface
Careercup its 1.8 serial shift includes problems
MySQL deep paging optimization with tens of millions of data, and online failure is rejected!
PostGIS installation geographic information extension
CLion配置visual studio(msvc)和JOM多核编译