Get the App
SLTechnology News&Howtos  ›  Development  › 

Analyze the misunderstanding of @ RequestParam (required = true)

Shulou Source: shulou.com Published: 2022-06-02 17:56:26 10月03日 Update

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!

Tags: Parameters front end misunderstanding type report analysis content time more knowledge practicality achievement and then code can make predicament reality situation article clear Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Huawei Docker Shulou Technology Shulou Tech Info