In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.