当前位置:网站首页>开发规范~参数校验异常、异常返回提示切面
开发规范~参数校验异常、异常返回提示切面
2022-06-24 19:38:00 【张子行的博客】
前言
曾几何时谁不是从一句 Hello Wrod 开始的秃头之旅呢,学习前期讲究代码能跑通就行,到了后期慢慢的开始去研究代码的优雅之道,什么设计模式啊、怎么使用最少的代码去实现一个功能。本文的内容在于帮助开发人员高效的对参数进行校验。
谏言
越简单的代码越高效,这个是我上岗遇到的第一个 IT 师傅说的,现已牢记于心也送给大家,不喜勿喷哦。
参数校验高级处理
直接编写环切的切面作用于参数校验不通过的异常就行,用到的就是 @ControllerAdvice 这个注解,至于 @ControllerAdvice 的生效时机,在我之前剖析Spring Mvc 源码的时候里面有详细的介绍。传送门
@Slf4j
@ControllerAdvice
@ResponseBody
public class MethodArgumentNotValidHandel {
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public Result MethodArgumentNotValidHandler(HttpServletRequest request, MethodArgumentNotValidException exception){
JSONObject errorMsg = new JSONObject();
for (FieldError error : exception.getBindingResult().getFieldErrors()) {
errorMsg.putOnce(error.getField(), error.getDefaultMessage());
log.error(request.getRequestURI() + " 参数校验错误:" + errorMsg);
}
return Result.failed(exception.getBindingResult().getFieldError().getDefaultMessage());
}
}
参数校验
直接在接口的接受参数上加上 @Validated 注解即可。至于校验
然后在TrainOrder上对需要校验的字段,加上注解校验即可,message为异常提示信息。
效果
当存在校验不通过的字段时,会返回该字段的异常提示信息,直至全部校验通过
边栏推荐
- Publicity of the second batch of shortlisted enterprises! Annual Top100 smart network supplier selection
- Leetcode: push domino (domino simulation)
- 1. fully explain the basic principles of IPSec
- How to compare two or more distributions: a summary of methods from visualization to statistical testing
- 04A中断的配置
- FANUC机器人_KAREL编程入门学习(1)
- HTTP的缓存控制
- Uncover the secret of station B. is it true that programmers wear women's clothes and knock code more efficiently?
- Technology inventory: Technology Evolution and Future Trend Outlook of cloud native Middleware
- Chapter 10 project stakeholder management
猜你喜欢

Docker installs redis-5.0.12. Detailed steps

Genesis public chain and a group of encryption investors in the United States gathered in consensus 2022

理想L9,智能座舱新潮流

Redis-跳表

NiO zero copy

String exercise summary 2

详细了解Redis的八种数据类型及应用场景分析
Relationnet++: a representation of fusion of multiple detection targets based on transformer | neurips 2020

NIO多路复用之Selector的使用

软件设计的七大原则
随机推荐
envoy获取客户端真实IP
Kubevela v1.2 release: the graphical operation console velaux you want is finally here
Industrial development status of virtual human
NIO 零拷贝
Principles of Ethernet port mirroring, link aggregation and VLAN Technology
Visitor tweets tell you which groups are consuming blind boxes
In the era of full programming, should I give up this road?
04A interrupt configuration
Huada 04A operating mode / low power consumption mode
MySQL + JSON = King fried!!
学习笔记23--多传感器信息融合基础理论(上)
Fanuc robot_ Introduction to Karel programming (1)
Common voting governance in Dao
A pit in try with resources
软件设计的七大原则
Why can some programmers get good offers with average ability?
[QT] QT event handling
Basic principles of layer 2 switching
In the multi network card environment, the service IP registered by Nacos is incorrect, resulting in inaccessible services
Technology Review: what is the evolution route of container technology? What imagination space is there in the future?