当前位置:网站首页>统一异常处理
统一异常处理
2022-06-11 19:38:00 【one_smail】
主要用到的注解:
@RestControllerAdvice:用于类上,标注该类作用于所有Controller
@ExceptionHandler:作用于具体方法上,标注该注解内标注异常触发的时候,执行该方法
示例:
import com.smile.dawncore.domain.AjaxResult;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.validation.ConstraintViolationException;
/**
* 同一异常处理
*/
@RestControllerAdvice
public class CommonExceptionHandler {
@ExceptionHandler({MethodArgumentNotValidException.class})
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public AjaxResult handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
BindingResult bindingResult = ex.getBindingResult();
StringBuilder sb = new StringBuilder("校验失败:");
for (FieldError fieldError : bindingResult.getFieldErrors()) {
sb.append(fieldError.getField()).append(":").append(fieldError.getDefaultMessage()).append(", ");
}
String msg = sb.toString();
return AjaxResult.error("参数校验失败:"+msg);
}
@ExceptionHandler({ConstraintViolationException.class})
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public AjaxResult handleConstraintViolationException(ConstraintViolationException ex) {
return AjaxResult.error("参数校验失败:"+ex.getMessage());
}
}边栏推荐
- "Case sharing" based on am57x+ artix-7 FPGA development board - detailed explanation of Pru Development Manual
- Introduction to typescript
- In 2021, the global barite product revenue was about $571.3 million, and it is expected to reach $710.2 million in 2028
- iMeta | 南科大夏雨组纳米孔测序揭示微生物可减轻高海拔冻土温室气体排放
- Multimodal learning toolkit paddlemm based on propeller
- 使用贝叶斯优化进行深度神经网络超参数优化
- Building web applications
- Key contents that wwdc22 developers need to pay attention to
- Off line operation of situation and policy (version) of Dayong in autumn 21 [standard answer]
- Pstack and dmesg
猜你喜欢

Introduction to go language (VI) -- loop statement

AHB2APB_bridge 设计

2022各大厂最新总结的软件测试宝典,看完不怕拿不到offer

YOLOv3 Pytorch代码及原理分析(一):跑通代码

Flutter--Button浅谈

Practice of Flink CDC in Dajian cloud warehouse

Record the phpstudy configuration php8.0 and php8.1 extension redis

基于飞桨搭建的多模态学习工具包PaddleMM

Merge multiple binary search trees

Understand how to get started with machine learning to quantify transactions?
随机推荐
[image denoising] impulse noise image denoising based on absolute difference median filter, weighted median filter and improved weighted median filter with matlab code attached
What is the workflow of dry goods MapReduce?
Chrome tips - browser web page setting coding, solving the problem of web page garbled code, obtaining the latest version of charset plug-in, UTF-8 coding setting
Golang学习笔记—基础篇
Raki's notes on reading paper: memory replace with data compression for continuous learning
巴比特 | 元宇宙每日必读:数字藏品二级市场乱象丛生,00后成新韭菜,监管迫在眉睫?...
[Lao Wang's fallacy of brain science] Why do blind people "seem" to be more "sensitive" than normal people?
09 MySQL lock
AHB2Standard_handshake_bridge 设计
Detailed explanation of iSCSI (IV) -- actual configuration of iSCSI server
Flash ckeditor rich text compiler can upload and echo images of articles and solve the problem of path errors
Hdu3527 (Hangdian) spy problem
Usage of duck beak wire stripper
Poj1028 web navigation
On high availability architecture
【C语言刷题——Leetcode10道简单题】
[solution] codeforces round 798 (Div. 2)
[video denoising] video denoising based on salt with matlab code
Loop filtering to uncover the technical principle behind video thousand fold compression
Practice of Flink CDC in Dajian cloud warehouse