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


边栏推荐
- Parker驱动器维修COMPAX控制器维修CPX0200H
- Careercup its 1.8 serial shift includes problems
- The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application
- Simple interest mode - lazy type
- int GetMonth( ) const throw( ); What does throw () mean?
- Uni app Bluetooth communication
- Prior knowledge of machine learning in probability theory (Part 1)
- 力扣------经营摩天轮的最大利润
- AITM2-0002 12s或60s垂直燃烧试验
- 树莓派4B上ncnn转换出来的模型调用时总是崩溃(Segment Fault)的原因
猜你喜欢

Using webassembly to operate excel on the browser side

基于 Ingress Controller 在集群外访问 Zadig 自测环境(最佳实践)

MySQL deep paging optimization with tens of millions of data, and online failure is rejected!

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

Clion-MinGW编译后的exe文件添加ico图标

Write an interface based on flask

Introduction of ArcGIS grid resampling method

EasyExcel的读写操作

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

Teach yourself to train pytorch model to Caffe (I)
随机推荐
Parker驱动器维修COMPAX控制器维修CPX0200H
WPF gets the control in the datagridtemplatecolumn of the specified row and column in the DataGrid
SYSTEMd resolved enable debug log
JMeter installation under win7
Is it necessary for bazel to learn
示波器探头对测量带宽的影响
Determine the best implementation of horizontal and vertical screens
Cross end solution to improve development efficiency rapidly
Aitm 2-0003 horizontal combustion test
Selenium's method of getting attribute values in DOM
ODPs next map / reduce preparation
Write an interface based on flask
Feng Tang's "spring breeze is not as good as you" digital collection, logged into xirang on July 8!
Deep merge object deep copy of vant source code parsing
判断横竖屏的最佳实现
Comprehensive optimization of event R & D workflow | Erda version 2.2 comes as "7"
Prior knowledge of machine learning in probability theory (Part 1)
Display DIN 4102-1 Class B1 fire test requirements
大二下个人发展小结
Introduction to TS, constructor and its this, inheritance, abstract class and interface