当前位置:网站首页>How to optimize a large number of if else [email protected] Detailed explanation of valib
How to optimize a large number of if else [email protected] Detailed explanation of valib
2022-07-26 12:46:00 【User 9919783】
1、 Business scenario
@RequestMapping("/insert")
public String insert(Student student){
if(Objects.equals(student.getName(),"")){
return " Name cannot be empty ";
}
if(null == student.getAge()){
return " Age cannot be empty ";
}
return "success";
}As shown in the figure , When the current end accesses the interface , Now it is required that the parameter cannot be empty , Then we can only write two if Judge whether it is null. if name When the access is empty , It will return as shown in the figure
At present, there are only two fields , What if there are a lot of fields to judge , Write a lot of if? When there is too much invalid business code in an interface , Design is problematic , And as a good programmer , This is not what we want to see .
How to solve it ? The answer is @Valid annotation
You can see from the code , First received bean Objects need to use @valid Verify the annotation , After that, we need to pay attention to ,bean Inside
The number type is @NotNull annotation , String type uses @NotBlank annotation , The set type is @NotEmpty Judge whether it exists . They judge the length of the annotation is also different , Namely @Length,@Range,@Size.
Be careful : When this is done , At the moment 【 It's not 】 Return to the front end and prompt what field is empty , You also need to add BindingResult Parameters , Receive abnormal data , Return an empty field to the front end .
边栏推荐
- What is the Internet of things? The most comprehensive explanation of common IOT protocols
- The database consists of stored procedures and functions
- 新功能 | 智能开放搜索上线定制词权重模型
- Redis主从复制原理
- Customize browser default right-click menu bar
- 2、 Container_
- 一款超好用的神器Apifox,甩 Swagger 几条街...(荣耀典藏版)
- 儿童玩乐场所如何运营?
- Optical distance sensing chip 4530a combining ambient light, proximity sensing and infrared ranging
- The significance of Internet of things device encryption
猜你喜欢
随机推荐
New function | intelligent open search online customized word weight model
Beauty salon management system unified management system?
The database consists of stored procedures and functions
.eslintrc.js configuration description
食品安全 | 这些常见食物小心有毒!速查自家餐桌
Kubernetes----Kubernetes常用插件简介
今日睡眠质量记录75分
mqtt send receive
STM32驱动HC05蓝牙串口通信模块
Kubernetes----安装部署NFS服务器
Azure synapse analytics Performance Optimization Guide (2) -- optimize performance using materialized views (Part 1)
回溯——第51题. N皇后——必须攻克的经典回溯难题
Paper reading MLPD: multi label pedestrian detector in multispectral domain (Internship Program of Hikvision Research Institute)
How does the chain store cashier system help shoe stores manage their branches?
数据库组成表
Analysis of Wireshark data package of network security B module of national vocational college skills competition Wireshark 0051.pcap
Implementation of dynamic and static libraries (packaging dynamic and static libraries for others to use)
敲黑板画重点:七种常见“分布式事务”详解
数据库组成存储过程和函数
Ds-112 time relay









