当前位置:网站首页>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, installation and use of postman tool
- Leetcode: calculate the number of elements less than the current element on the right (sortedlist+bisect\u left)
- Future development of education industry of e-commerce Express
- OA system -- save the verification code to session
- Web security XSS foundation 06
- Description of software version selection of kt6368a Bluetooth dual-mode transparent chip
- 【WSL】SSH 远程连接及宿主机端口转发配置
- Based on the codeless platform, users deeply participated in the construction, and digital data + Nanjing Fiberglass Institute jointly built a national smart laboratory solution
- 堆内存分配的并发问题
- Cache control of HTTP
猜你喜欢

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

In the multi network card environment, the service IP registered by Nacos is incorrect, resulting in inaccessible services

Introduction to machine learning compilation course learning notes lesson 1 overview of machine learning compilation

1. fully explain the basic principles of IPSec

AQS source code analysis

网上立案流程

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

Win10 or win11 printer cannot print

Docker 安装 MySQL 8.0,详细步骤

Panorama of enterprise power in China SSD industry
随机推荐
双亲委派机制
Why can some programmers get good offers with average ability?
win10或win11打印机无法打印
Idea global search replace shortcut key
NiO zero copy
2022-06-10 工作记录--JS-获取到某一日期N天后的日期
04A interrupt configuration
Use of selector for NiO multiplexing
Firewall working principle and detailed conversation table
Docker 安装 Redis-5.0.12,详细步骤
envoy获取客户端真实IP
seven
Huada 4a0gpio settings
DX 的 HLSL 和 GL 的 GLSL的 矩阵构建的行列区别
上新了,华为云开天aPaaS
Interrupt, interrupted, isinterrupted differences
Selection and comparison of message oriented middleware MQ
NiO, bio, AIO
Docker installs MySQL 8.0. Detailed steps
Industrial development status of virtual human