当前位置:网站首页>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
边栏推荐
- 学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
- vant 源码解析 之深层 合并对象 深拷贝
- int GetMonth( ) const throw( );后面的throw( )什么意思?
- Mode - "Richter replacement principle"
- Generics of TS
- Hdu2377bus pass (build more complex diagram +spfa)
- 基于 Ingress Controller 在集群外访问 Zadig 自测环境(最佳实践)
- Two ways to realize video recording based on avfoundation
- Display DIN 4102-1 Class B1 fire test requirements
- Explain various hot issues of Technology (SLB, redis, mysql, Kafka, Clickhouse) in detail from the architecture
猜你喜欢
Chapter 05_ Storage engine
Uni app Bluetooth communication
Realize the function of verifying whether the user has completed login when browsing the page
基于 Ingress Controller 在集群外访问 Zadig 自测环境(最佳实践)
Access Zadig self-test environment outside the cluster based on ingress controller (best practice)
Explain various hot issues of Technology (SLB, redis, mysql, Kafka, Clickhouse) in detail from the architecture
ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图
显示器要申请BS 476-7 怎么送样?跟显示屏一样吗??
MQ----activeMq
Feng Tang's "spring breeze is not as good as you" digital collection, logged into xirang on July 8!
随机推荐
systemd-resolved 开启 debug 日志
显示器要申请BS 476-7 怎么送样?跟显示屏一样吗??
Influence of oscilloscope probe on measurement bandwidth
Aitm 2-0003 horizontal combustion test
How to prepare for the algorithm interview and answer the algorithm interview questions
MySQL 千万数据量深分页优化, 拒绝线上故障!
Write an interface based on flask
Golang (1) | from environmental preparation to quick start
Arcgis\qgis no plug-in loading (no offset) mapbox HD image map
123456
Aitm2-0002 12s or 60s vertical combustion test
EasyExcel的读写操作
EN 438-7 laminated sheet products for building covering decoration - CE certification
Talk about my fate with some programming languages
vant 源码解析之 utils/index.ts 工具函数
判断横竖屏的最佳实现
LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line
EasyExcel的讀寫操作
int GetMonth( ) const throw( ); What does throw () mean?
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