当前位置:网站首页>大量if else判断如何优化?@Valib详解
大量if else判断如何优化?@Valib详解
2022-07-26 11:46:00 【用户9919783】
1、业务场景
@RequestMapping("/insert")
public String insert(Student student){
if(Objects.equals(student.getName(),"")){
return "姓名不能为空";
}
if(null == student.getAge()){
return "年龄不能为空";
}
return "success";
}如图所示,当前端访问接口时,现在要求参数不能为空,那我们只能这样写两个if判断是否为null。若name访问为空时候,则会如图返回
目前现在只有两个字段,若有大量的字段需要判断则如何呢,写大量的if?当一个接口里无效业务代码量过多,设计是有问题的,而且作为一个优秀的程序员,这不是我们想看到的结果。
如何解决呢?答案是@Valid注解
从代码里可以看到,首先接收的bean对象需要用@valid注解验证,之后需要注意的是,bean里面
数字类型用@NotNull注解,字符串类型用@NotBlank注解,集合类型用的是@NotEmpty判断是否存在。他们判断长度的注解也不同,分别是@Length,@Range,@Size。
注意:这里完成之后,此刻【并不会】返回前端并且提示什么字段为空,还需要在接口加BindingResult参数,接收异常数据,返回空字段给前端。
边栏推荐
- 专访即构科技李凯:音视频的有趣、行业前沿一直吸引着我
- 3.1 创建菜单与游戏页面——上
- PyCharm是真的强
- 10 个 Reduce 常用“奇技淫巧”
- [cloud co creation] what good habits do you adhere to in order to write good code?
- Esp8266 Arduino programming example - know esp8266
- .....
- 浅谈低代码技术在物流运输平台中的搭建与管理
- [ten thousand words long text] Based on LSM tree thought Net 6.0 C # realize kV database (case version)
- System call capture and segmentation - RING3 layer LD_ Preload mechanism for library function hijacking
猜你喜欢

The latest heart-shaped puzzle applet source code + with flow master

What is per title encoding?

。。。。。。
![[early knowledge of activities] list of recent activities of livevideostack](/img/a5/06c13865b7adbd99d43c1a52f3fc4d.png)
[early knowledge of activities] list of recent activities of livevideostack

Pytoch -- error based on mmseg/mmdet training: runtimeerror: expected to have finished reduction in the priority iteration

Redis database, which can be understood by zero foundation Xiaobai, is easy to learn and use!

了解 useRef 一篇就够了

Ga-rpn: recommended area network for guiding anchors

基于 Flink CDC 实现海量数据的实时同步和转换

Hashtable
随机推荐
MILA旗下初创企业Ubenwa获得250万美元投资,研究婴儿健康AI诊断
Wulin headlines - station building expert competition
.....
PostgreSQL in Linux and windows installation and introductory basic tutorial
Data Lake (19): SQL API reads Kafka data and writes it to iceberg table in real time
How to use data pipeline to realize test modernization
win10使用nvm安装node、npm、cnpm
PyCharm是真的强
[ten thousand words long text] Based on LSM tree thought Net 6.0 C # realize kV database (case version)
Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 8
系统调用捕获和分析—Ring0层kprobe劫持系统调用
pytest接口自动化测试框架 | 重新运行失败用例
Exploration on cache design optimization of community like business
Understanding useref is enough
Which is faster to open a file with an absolute path than to query a database?
如何使用数据管道实现测试现代化
【倒计时10天】腾讯云音视频专场即将见面,千元大奖等你来拿!
Programmer growth chapter 28: how can managers not do it by themselves?
Li Kai: the interesting and cutting-edge audio and video industry has always attracted me
Server memory failure prediction can actually do this!