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


边栏推荐
- Clickhouse copy paste multi line SQL statement error
- one hundred and twenty-three thousand four hundred and fifty-six
- Clion-MinGW编译后的exe文件添加ico图标
- When a user logs in, there is often a real-time drop-down box. For example, entering an email will @qq com,@163. com,@sohu. com
- Simple interest mode - lazy type
- [daily training -- Tencent select 50] 89 Gray code (only after seeing the solution of the problem)
- Careercup its 1.8 serial shift includes problems
- 让开发效率飞速提升的跨端方案
- WPF gets the control in the datagridtemplatecolumn of the specified row and column in the DataGrid
- JS common method encapsulation
猜你喜欢

Clickhouse copy paste multi line SQL statement error

leetcode:1139. The largest square bounded by 1

Simple interest mode - evil Chinese style

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

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

校招期间 准备面试算法岗位 该怎么做?

uni-app 蓝牙通信

Teach yourself to train pytorch model to Caffe (I)

CLion配置visual studio(msvc)和JOM多核编译

EN 438-7 laminated sheet products for building covering decoration - CE certification
随机推荐
selenium 获取dom内验证码图片
Wood board ISO 5660-1 heat release rate mapping test
Simple interest mode - lazy type
Selenium's method of getting attribute values in DOM
Pytoch practice -- MNIST dataset handwritten digit recognition
阿里云有奖体验:用PolarDB-X搭建一个高可用系统
Write an interface based on flask
Binary search
js常用方法封装
EN 438-7 laminated sheet products for building covering decoration - CE certification
[case] Application of positioning - Taobao rotation map
The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application
Arcgis\qgis no plug-in loading (no offset) mapbox HD image map
Realize the function of verifying whether the user has completed login when browsing the page
Prior knowledge of machine learning in probability theory (Part 1)
Introduction to TS, constructor and its this, inheritance, abstract class and interface
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
Aitm2-0002 12s or 60s vertical combustion test
Is Kai Niu 2980 useful? Is it safe to open an account
学习机器人无从下手?带你体会当下机器人热门研究方向有哪些