Get the App
SLTechnology News&Howtos  ›  Development  › 

How does springboot read the parameters in the url address of a http request

Shulou Source: shulou.com Published: 2022-06-03 08:55:23 09月21日 Update

What this article shares with you is about how springboot reads the parameters in the http request url address. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

When defining a Rest interface, GET, POST, PUT and DELETE are usually used to add, delete, modify and query data. Some of these methods need to pass parameters, and background developers must verify the parameters received to ensure the robustness of the program.

GET: generally used to query data, transmitted in clear text, and generally used to obtain some data that has nothing to do with user information.

POST: commonly used to insert data

PUT: commonly used for data updates

DELETE: generally used for data deletion; usually for logical deletion (that is, only changing the state of the record, not actually deleting the data)

1. @ PathVaribale to get the data in url

Request URL:localhost:8080/hello/id to get id value

The implementation code is as follows:

/ / java project www.fhadmin.org@RestControllerpublicclass HelloController {@ RequestMapping (value= "/ hello/ {id} / {name}", method= RequestMethod.GET) public String sayHello (@ PathVariable ("id") Integer id,@PathVariable ("name") String name) {return "id:" + id+ "name:" + name;}}

Enter the address in the browser:

Localhost:8080/hello/100/hello

Output:

Id:81name:hello

2. @ RequestParam gets the value of the request parameter

Get the url parameter value. By default, the method parameter name is required to be consistent with the url parameter.

Request URL:localhost:8080/hello?id=1000

/ / java project www.fhadmin.org@RestControllerpublicclass HelloController {@ RequestMapping (value= "/ hello", method= RequestMethod.GET) public String sayHello (@ RequestParam Integer id) {return "id:" + id;}}

Output: id:100

When there are multiple parameters in url, such as:

Localhost:8080/hello?id=98&&name=helloworld

The specific code is as follows:

/ / java project www.fhadmin.org@RestControllerpublicclass HelloController {@ RequestMapping (value= "/ hello", method= RequestMethod.GET) public String sayHello (@ RequestParam Integer id,@RequestParam String name) {return "id:" + id+ "name:" + name;}}

Get the url parameter value and execute the parameter name method

Localhost:8080/hello?userId=1000

/ / java project www.fhadmin.org@RestControllerpublicclass HelloController {@ RequestMapping (value= "/ hello", method= RequestMethod.GET) public String sayHello (@ RequestParam ("userId") Integer id) {return "id:" + id;}}

Output: id:100

This is how springboot reads the parameters in the url address of the http request. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Parameters data project address method output code name more knowledge articles robust practical consistent people information background multiple that is work meeting Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Huawei Docker Xiaomi Apple