当前位置:网站首页>统一异常处理
统一异常处理
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());
}
}边栏推荐
- Merge sort divide and conquer
- 2022各大厂最新总结的软件测试宝典,看完不怕拿不到offer
- Qubicle notes: Hello voxel
- Activate function formulas, derivatives, image notes
- [image denoising] image denoising based on Markov random field with matlab code
- 管理者必须知道的三个常识
- PIL pilot image processing [1] - installation and creation
- Introduction to go language (VI) -- loop statement
- Pyqt5 tips - button vertical display method, QT designer sets button vertical display.
- Off line operation of situation and policy (version) of Dayong in autumn 21 [standard answer]
猜你喜欢

谷歌提出超强预训练模型CoCa,在ImageNet上微调Top-1准确率达91%!在多个下游任务上SOTA!...

Merge multiple binary search trees

Understand how to get started with machine learning to quantify transactions?

Operator new and placement new

Key contents that wwdc22 developers need to pay attention to

Yolov3 pytoch code and principle analysis (I): runthrough code

MOS transistor 24n50 parameters of asemi, 24n50 package, 24n50 size

Flutter--Button浅谈

APB2standard_handshake_bridge 设计

Common - name of conference room
随机推荐
"Case sharing" based on am57x+ artix-7 FPGA development board - detailed explanation of Pru Development Manual
基于 Vue + Codemirror 实现 SQL 在线编辑器
[laravel series 7.5] event system
Key contents that wwdc22 developers need to pay attention to
High performance architecture design
LDPC 7 - simple example of decoding
Raki's notes on reading paper: learning fast, learning slow: a general continuous learning method
Practice of Flink CDC in Dajian cloud warehouse
This article introduces you to the infrastructure of sofaregistry
Activate function formulas, derivatives, image notes
SISO decoder for repetition (supplementary Chapter 4)
Pymysql uses cursor operation database method to encapsulate!!!
YOLOv3 Pytorch代码及原理分析(一):跑通代码
2022各大厂最新总结的软件测试宝典,看完不怕拿不到offer
Learn about random library · 1
NR LDPC punched
MySQL——事务
Go语言入门(六)——循环语句
How are functional components different from class components
AHB2APB_bridge 设计