Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analyze the misunderstanding of @ RequestParam (required = true)

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "analyzing the misunderstanding of @ RequestParam (required = true)". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The misunderstanding of @ RequestParam (required = true) begins with the conclusion.

Required = true reports an error when no parameter is passed in the front end, and does not prevent the parameter from being empty.

Consider the code:

GetMapping ("/ userInfo") public void userInfo (@ RequestParam (required = true) Integer id) {.}

A 400 error will be reported if the request is as follows:

Http://localhost:8088/userInfo

However, the following request can be constructed to make id empty:

Http://localhost:8088/userInfo?id=

Parameter summary

Required=true indicates that the front end must pass parameters.

Required=false means that when no parameter is passed at the front end, the parameter will be set to null. Therefore, if the parameter is of a type that cannot be assigned to null, int, an error may be reported.

If defaultValue,required is used, it can only be false. If no parameter is passed at the frontend, the parameter will be set to defaultValue.

@ RequestParam (required=false int id) value does not report an error

@ RequestParam (required=false int id): required=false indicates that there is no need to pass a value, if it is

/ / Note that it is now id@RequestParam (required=false Sting id) of type String.

If the value is not transmitted, the error will not be reported.

But if it's the int type written on the title, this

@ RequestParam (required=false int id)

An error will be reported because null cannot be assigned to int.

That's all for "analyzing the misunderstandings of @ RequestParam (required = true)". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development