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

How to use springboot to get controller parameters

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to use springboot to get controller parameters, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

For example, there are four ways for springboot to obtain controller parameters.

1. Get parameters without comments

2. Use @ RequestParam to get parameters

3. Pass the array

4. Pass parameters through URL

Get parameters without annotations

To obtain the parameters without comments, the controller layer parameters need to be the same as the HTTP request column parameter name, so that the backend can obtain the parameters of the request column.

/ * * when getting parameters without comments The parameter name must be consistent with the HTTP request parameter * @ param name String name * @ param age int age * @ param score double score * @ return response json character-@ ResponseBody annotation to change map to json * / @ RequestMapping ("/ param/noannotation") @ ResponseBody public Map noAnnotation (String name,int age) Double score) {Map paramMap = new HashMap () ParamMap.put ("name", name); paramMap.put ("age", age); paramMap.put ("score", score); return paramMap;}

In the method, we receive three parameters, name,age and score

After starting springboot, request URL: http://localhost:8080/param/noannotation?name=zhangsan&age=14&score=89.1 in the browser

The front-end page will automatically get the json form of the parameters we enter

If the name in our request URL does not give the parameter value http://localhost:8080/param/noannotation?name=&age=12&score=89.1

The request can jump normally.

If we leave the int or long parameter empty, give it to URL http://localhost:8080/param/noannotation?name=lisi&age=12&score=

The request will report an error.

Unable to convert string type parameter to required string type.

As you can see here, if we do not assign a value to the parameter, spring will default to the empty parameter as a string-type empty string. Therefore, if it is a string parameter, it will not report an error when it is empty; if it is a non-string parameter, it must be non-empty.

Use @ RequestParam to get parameters

In the absence of comments above, the HTTP parameter must be consistent with the controller parameter name. If the HTTP parameters are not consistent with the controller parameters, we need to correspond the front-end parameters to the back-end parameters. Here we use @ RequestParam to determine the mapping of the front-end and back-end parameter names.

/ * * use @ RequestParam to obtain parameters * here the RequestParam annotation converts the front-end parameter name in parentheses to the following parameter name * @ param name String name, receives the front-end username parameter * @ param age int age, and receives the front-end user_age parameter * @ param score double score Receive the front-end score parameter * @ return response json character * / @ RequestMapping ("/ param/requestparam") @ ResponseBody public Map requestParam (@ RequestParam ("username") String name, @ RequestParam ("user_age") int age @ RequestParam ("score") double score) {Map paramMap = new HashMap () ParamMap.put ("name", name); paramMap.put ("age", age); paramMap.put ("score", score); return paramMap;}

Method, we receive the front-end username,user_age and score, and then use the @ RequestParam annotation to map them to name,age and age.

After starting springboot, enter http://localhost:8080/param/requestparam?username=lisi&user_age=12&score=45.6 in the URL field

It can jump normally and display the result on the front end.

If the value after username in URL is left empty, the page can jump normally and test the URL.

Http://localhost:8080/param/requestparam?username=&user_age=12&score=45.6

If we leave the user_age or score value empty, the page will report an error

The error is the same as when there is no comment above, and there is no explanation here.

If we delete the username parameter item in the URL column, URL is http://localhost:8080/param/requestparam?user_age=12&score=45.6

The page cannot jump normally, and the error is as follows

What if, in practice, we do have front-end parameters that can be passed or not? The @ RequestParam annotation gives a required attribute indicating whether the parameter item is required. Its default value is true, that is, the value must be passed; if the value may be empty, set required to false. Note that at this point, the data type after @ RequestParam must be a wrapper type, not a simple type. If it is a simple type, it will still report an error. When wrapping a type, you can receive null objects; if it is a simple type, the system will still make a strong conversion between the empty string and the corresponding simple type, and if the conversion fails, an exception will be thrown.

We partially modify the above method parameters as follows. Because of the space, we will no longer post the whole method as a whole.

RequestMapping ("/ param/requestparam") @ ResponseBody public Map requestParam (@ RequestParam ("username") String name, @ RequestParam (value = "user_age", required = false) Integer age, @ RequestParam (value = "score", required = false) double score) {

Above, we change the corresponding type of age to Integer wrapper type, and set required to false;score, but only set required to false.

Start springboot for testing, http://localhost:8080/param/requestparam?username=zhangsan&user_age=&score=89

Here, we set the parameter value after user_age to empty, and the request can jump normally.

Let's modify the URL parameter item, http://localhost:8080/param/requestparam?username=zhangsan&user_age=&score=.

The system will report a 400th error, indicating that score is an empty string. If you change the data type double to Double, it will return null. This will not be tested here.

Receive the array passed by the front end

In springMVC, in addition to passing some single values from the front end to the back end, you can also pass arrays. There are not many rules for transferring data. In the background controller, define the array form to receive data, and when transmitting the front end, you can separate a set of data with a comma in English.

RequestMapping ("/ param/requestarray") @ ResponseBody public Map requestArray (String [] names, int [] ages, double [] scores) {Map paramMap = new HashMap (); paramMap.put ("name", names); paramMap.put ("age", ages); paramMap.put ("score", scores); return paramMap;}

Start the springboot,URL as

Http://localhost:8080/param/requestarray?names=zhangsan,lisi,wangwu&ages=12,14,15&scores=78.9,98,67

We use names to receive all name with multiple name values "zhangsan", "lisi" and "wangwu" separated by ","

The page can jump normally.

Passing parameters through URL

The parameter passed through URL is different from the above URL. The above URL needs to specify the parameter name and corresponding parameter value. For the URL to pass the parameter, you only need to enter the parameter value in the address bar, and then automatically match the corresponding parameter name to the backend. SpringMVC obtains the URL parameter by using a combination of processor mapping and the @ PathVariable annotation. First, you can locate the location and name of the parameter through the processor mapping, while @ PathVariable can get the parameter by name.

/ * when passing parameters using URL * URL, the method requests URL and uses {} to include the parameter name. Use / separate * @ param name name * @ param age age * @ param score score * @ return response json data * / @ RequestMapping ("/ param/geturl/ {name} / {age} / {score}") @ ResponseBody public Map getUrlParam (@ PathVariable ("name") String name between multiple parameters @ PathVariable ("age") Integer age, @ PathVariable ("score") Double score) {Map paramMap = new HashMap () ParamMap.put ("name", name); paramMap.put ("age", age); paramMap.put ("score", score); return paramMap;}

In the method, we use three {} to represent the variables name, age and score, respectively. @ PathVariable is used in the method variable to receive the variable and map it to the method variable.

Start the springboot test, URL is http://localhost:8080/param/geturl/zhangsan/12/89

Normal jump

If one of the middle parameters may be empty, check that the @ PathVariable annotation has a required attribute, but after testing, it is found that this attribute cannot be used alone.

If one of the middle parameters is empty, you have to borrow the @ RequestMapping attribute that supports multi-URL for processing.

If the age property may be empty, modify the code to

@ RequestMapping (value = {"/ param/geturl/ {name} / {age} / {score}", "/ param/geturl/ {name} / {score}"}) @ ResponseBody public Map getUrlParam (@ PathVariable ("name") String name, @ PathVariable (value = "age", required = false) Integer age @ PathVariable ("score") Double score) {

There is no age parameter in the later URL. Here, the request with or without age is regarded as a different request.

Use URL http://localhost:8080/param/geturl/zhangsan//89 to do the test, you can jump normally

The above is all the contents of the article "how to use springboot to get controller parameters". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report