当前位置:网站首页>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
边栏推荐
- Introduction to machine learning compilation course learning notes lesson 1 overview of machine learning compilation
- [personal experiment report]
- Unable to use the bean introduced into the jar package
- Docker installs MySQL 8.0. Detailed steps
- Idea global search replace shortcut key
- cat写多行内容到文件
- CDN principle
- Panorama of enterprise power in China SSD industry
- Docker installs redis-5.0.12. Detailed steps
- 2022-06-16 work record --js- judge the number of digits in string type digits + judge the number of digits in numeric type digits + limit the text length (display n words at most, exceeding...)
猜你喜欢

软件设计的七大原则

Cross border e-commerce, early entry and early benefit

FANUC机器人_KAREL编程入门学习(1)

Seven principles of software design

The ktp900f mobile download program of the fail safe mobile panel prompts that the download cannot be performed, and the target device is running or not in the transmission mode

Redis-跳表

Unable to use the bean introduced into the jar package

Description of software version selection of kt6368a Bluetooth dual-mode transparent chip

NIO 零拷贝

win10或win11打印机无法打印
随机推荐
Unable to use the bean introduced into the jar package
STP spanning tree protocol Foundation
cat写多行内容到文件
Industrial development status of virtual human
Chapter 10 project communication management
Technology Review: what is the evolution route of container technology? What imagination space is there in the future?
Yyds dry goods inventory junit5 learning II: assumptions class
面试害怕被问MySQL相关问题 ?这份三万字精华总结 + 面试100 问,吊打面试官完全够了
详细了解Redis的八种数据类型及应用场景分析
软件设计的七大原则
JMM 最最最核心的概念:Happens-before 原则
Description of transparent transmission function before master and slave of kt6368a Bluetooth chip, 2.4G frequency hopping automatic connection
Dynamic memory management (1)
【软件工程】期末重点
开发规范~参数校验异常、异常返回提示切面
Docker installs MySQL 8.0. Detailed steps
The difference between interceptor and filter
网上立案流程
find your present (2)
Creating files, recursively creating directories