当前位置:网站首页>Development specification - parameter verification exception, exception return prompt section
Development specification - parameter verification exception, exception return prompt section
2022-06-24 22:45:00 【Zhang Zixing's blog】
Preface
Once upon a time, who did not start from a sentence Hello Wrod The beginning of the bald journey , In the early stage of learning, you should pay attention to that the code can run smoothly , In the later stage, I began to study the elegant way of code , What design pattern 、 How to use the least code to implement a function . The content of this article is to help developers verify parameters efficiently .
Remonstrance
The simpler the code, the more efficient , This is the first one I met on duty IT Shifu said , Now I have kept it in mind and give it to you , Don't spray if you don't like it .
Advanced processing of parameter verification
Directly write the tangent plane of the loop cut to act on exceptions that fail the parameter verification , What we use is @ControllerAdvice This annotation , as for @ControllerAdvice Time to take effect , Before I dissect Spring Mvc There is a detailed introduction to the source code . Portal
@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() + " Parameter check error :" + errorMsg);
}
return Result.failed(exception.getBindingResult().getFieldError().getDefaultMessage());
}
}
Parameter checking
Add... Directly to the accepted parameters of the interface @Validated Annotations can be . As for verification 
And then in TrainOrder On the field to be verified , Add notes to verify ,message Is an exception prompt .
effect
When there are fields that fail the verification , The exception prompt information of this field will be returned , Until all the verification passes
边栏推荐
- Huada 4a0gpio settings
- Principle of IP routing
- Code farmers should also understand the IPv4 subnet division of point networks
- Tetris
- Cross border e-commerce, early entry and early benefit
- Row and column differences in matrix construction of DX HLSL and GL glsl
- JMM 最最最核心的概念:Happens-before 原则
- Web攻击之CSRF和SSRF
- Power system | IEEE paper submission process
- String exercise summary 2
猜你喜欢

结合源码剖析Oauth2分布式认证与授权的实现流程

别再乱用了,这才是 @Validated 和 @Valid 的真正区别!!!

Principles of Ethernet port mirroring, link aggregation and VLAN Technology

envoy获取客户端真实IP

【Mongodb】READ_ME_TO_RECOVER_YOUR_DATA,数据库被恶意删除

See how sparksql supports enterprise level data warehouse

Seven principles of software design

Row and column differences in matrix construction of DX HLSL and GL glsl

NIO多路复用之Selector的使用

糖豆人登录报错解决方案
随机推荐
2022-06-16 工作记录--JS-判断字符串型数字有几位 + 判断数值型数字有几位 + 限制文本长度(最多展示n个字,超出...)
[personal experiment report]
重磅!法大大上榜“专精特新”企业
interrupt、interrupted 、isInterrupted 区别
Tetris
Nuscenes -- remedies for missing image files or 0-size images encountered during dataset configuration
img2pdf
【軟件工程】期末重點
Learn more about the practical application of sentinel
【Mongodb】READ_ME_TO_RECOVER_YOUR_DATA,数据库被恶意删除
OSPF basic content
上新了,华为云开天aPaaS
Kubevela v1.2 release: the graphical operation console velaux you want is finally here
磁盤的結構
Learning bit segment (1)
Concurrency of heap memory allocation
Description of software version selection of kt6368a Bluetooth dual-mode transparent chip
ThreadLocal内存泄漏问题
High level application of SQL statements in MySQL database (II)
img2pdf