当前位置:网站首页>Detailed introduction of @RequestParam annotation
Detailed introduction of @RequestParam annotation
2022-07-30 00:34:00 【Just a small front end】
@RequestParam (org.springframework.web.bind.annotation.RequestParam) is used to assign the specified request parameter to the formal parameter in the method.
There are three properties:
(1) value: request parameter name (must be configured)
(2) required: Whether it is required, the default is true, that is, this parameter must be included in the request, if not included, an exception will be thrown (optional configuration)
(3) defaultValue: default value, if this value is set, required will be automatically set to false, no matter whether you configure required or not, and what value is configured, it is false (optional configuration)
1 In order to demonstrate the effect, first you need to create a test control class, and then create a test method, _As shown in the figure, add a parameter to the _____ method, ____At this time, the method parameterNo annotations added

Start the web project through the idea tool, and then access the test effect through the browser. If there is no parameter, the obtained parameter is null, but the page can be displayed normally and the request can be executed normally.

After adding the parameters, the parameters can be normal

Let's compare the effect of adding annotations, but just add @RequestParam without any annotations to set parameters.

View the effect through the browser again. At this time, the page with the userName parameter can be displayed normally and the result value can be printed out, but as shown in the figure, when the parameter is not used, the page reports an error, returns 400, and promptsuserName does not exist


Of course, such restrictions are optional. Add required=false to @RequestParam to close the restrictions on required parameters, which is the same as the first one without annotations


@RequestParam can also set a default value of a parameter for us when the parameter is empty, as shown in the figure, it can be seen that if there is no parameter, the obtained value is the default value
p>The key name of the default parameter is the parameter name in the method, but another one can be customized through @RequestParam, because under certain conditions, the method name cannot be modified after being fixed, but the requested parameters need to bePass another parameter key name, so it can be implemented in this way



边栏推荐
猜你喜欢

每周推荐短视频:研发效能是什么?它可以实现反“内卷”?

消息中间件解析 | 如何正确理解软件应用系统中关于系统通信的那些事?

Worthington解离酶:胰蛋白酶及常见问题

Print linked list from end to beginning

Mysql internal and external connections

旋转数组的最小数字

The strongest JVM in the whole network is coming!(Extreme Collector's Edition)

【微服务~Nacos】Nacos之配置中心

Finding a 2D Array

Worthington木瓜蛋白酶&胰凝乳蛋白酶&脱氧核糖核酸酶 I
随机推荐
How Filebeat ensures that the log file is still correctly read when the log file is split (or rolled)
[MySQL series] MySQL database foundation
ZLMediaKit源码学习——UDP
【分层强化学习】survey
【集训DAY18】有趣的交换【模拟】【数学】
工厂模式
Replace the executable file glibc version of the one
Vmtouch - under Linux file cache management artifact
Unity Addressables
【Flutter】混合开发之Flutter预加载解决第一次加载页面缓慢问题
Worthington解离酶:中性蛋白酶(分散酶)详情解析
头条号自媒体运营:如何在今日头条涨500+粉丝?
News text classification
Towhee 每周模型
One article to answer web performance optimization
Douyin short video traffic acquisition strategy, mastering these will definitely be a hit
Introduction to Worthington Elastase & Hyaluronidase
Go日志库——logrus
转发和重定向的区别及使用场景
[Training DAY16] ALFA [convex hull] [computational geometry]