In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the springboot RESTful and parameter annotation example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Springboot RESTful and parameter annotations using RESTful
The complexity of Spring does not come from the objects it deals with, but from itself. The evolving Spring will bring complexity in time dimension, such as @ RequestMapping in the previous version of SpringMVC. When the new version is replaced by the following new comments, it is equivalent to an added option:
@ GetMapping@PostMapping@PutMapping@DeleteMapping@PatchMapping
Description
1. @ GetMapping
Abbreviation for @ RequestMapping (method = RequestMethod.GET)
Function: corresponding to the query, indicating that it is a query URL mapping
2. @ PostMapping
Abbreviation for @ RequestMapping (method = RequestMethod.POST)
Function: corresponding to the increase, indicating that it is an increased URL mapping
3. @ PutMapping
Abbreviation for @ RequestMapping (method = RequestMethod.PUT)
Function: corresponds to an update, indicating that it is an updated URL map
4. @ DeleteMapping
Abbreviation for @ RequestMapping (method = RequestMethod.DELETE)
Function: corresponding to deletion, indicating that it is a deleted URL mapping
5. @ PatchMapping
Patch mode is a supplement to put mode.
Put mode can be updated. But the update is the whole. The patch is a local update.
Use of parameter annotations @ PathVariable@RequestParam@RequestBody@ModelAttribute
Description
1. @ PathVariable
Gets the path parameters. In the form of url/ {id}
@ PathVariable binds the URI template variable value
@ PathVariable is used to obtain the dynamic parameters in the request url
PathVariable is used to map the template variables in the request URL to the parameters of the function processing method. / / configure a relationship between url and method @ RequestMapping ("item/ {itemId}")
2.@RequestParam
Gets the query parameters. In the form of url?name=
What are the main parameters of @ RequestParam annotation:
Value: parameter name, that is, the request parameter name of the input parameter. For example, username indicates that the value of the parameter whose name is username in the request parameter area will be passed
Required: whether it is required. The default is true, which means there must be a corresponding parameter in the request, otherwise an error code of 404 will be reported.
DefaultValue: default value, which indicates the default value if there is no parameter with the same name in the request, for example:
Public List getItemTreeNode (@ RequestParam (value= "id", defaultValue= "0") long parentId) 3.@RequestBody
The @ requestBody annotation is often used to deal with content that content-type is not the default application/x-www-form-urlcoded encoding, such as application/json or application/xml. In general, it is often used to deal with application/json types.
With @ requestBody, you can bind the JSON string in the request body to the corresponding bean, or you can bind them to the corresponding string respectively.
4.@ModelAttribute
When using the RESTful style, you can use this annotation if you use a get request and want to use an object to receive parameters
Applies not only to get requests, but also to put and delete requests
Springboot Restful usage record
Create a project
Create a project through the spring official website
Https://start.spring.io/
The project name is studyRest
The project depends on WEB
Rest components use the
Use @ RestController to mark the class as the Contoller that provides Restful services
@ GetMapping is part of the resource location, that is, url, which corresponds to http://localhost:8080/test
@ RestControllerpublic class MyRestContoller1 {@ GetMapping ("/ test") public Map getData () {Map data = new HashMap (); data.put ("id", "111"); data.put (" name "," zhangsan "); return data;}}
Testing (browser testing is used here and Postman tool is used later)
The @ GetMapping keyword corresponds to a GET request, that is, a query. The request can also have parameters, corresponding to @ PathVariable and @ RequestParam annotations.
@ GetMapping ("/ test/ {id}") public Map getData2 (@ PathVariable String id, @ RequestParam (required = false) String name) {Map data = new HashMap (); data.put ("id", id); data.put ("name", name); return data;}
Test. The returned value is input parameter.
Post type, new operation
Added @ PostMapping to describe URL
Generally, a large amount of data is added with the @ RequestBody annotation to encapsulate parameters.
@ PostMapping ("/ test2/add") public Map addData (@ RequestBody Map data) {return data;}
test
Pay attention to two points, incorrect will report an error
Request type must be POST
Content-type must be set to application/json because the input parameter is in JSON format
Update and delete operation
It is consistent with Post in use, except that different types need to use corresponding hosts.
PUT:@PutMapping
DELETE:@DeleteMapping
@ PutMapping ("/ test2/update") public Map updateData (@ RequestBody Map data) {return data;} @ DeleteMapping ("/ test2/delete") public Map deleteData (@ RequestBody Map data) {return data;} RequestMapping use
RequestMapping is a general annotation that includes all of the above operations
@ RestController@RequestMapping (value = "/ parent") public class RequestRestContoller {@ RequestMapping (value = "/ get", method = RequestMethod.GET) public Map get () {Map data = new HashMap (); data.put ("id", "111"); data.put (" name "," zhangsan "); return data } @ RequestMapping (value = "/ add", method = RequestMethod.POST) public Map add () {Map data = new HashMap (); data.put ("id", "111"); data.put (" name "," zhangsan "); return data } @ RequestMapping (value = "/ update", method = RequestMethod.PUT) public Map update () {Map data = new HashMap (); data.put ("id", "111"); data.put (" name "," zhangsan "); return data } @ RequestMapping (value = "/ delete", method = RequestMethod.DELETE) public Map delete () {Map data = new HashMap (); data.put ("id", "111"); data.put (" name "," zhangsan "); return data;}}
The above also has a note posted on class: @ RequestMapping (value = "/ parent"). If it is a note on class, then the url above the method needs to be added with the note on class.
Such as: http://localhost:8080/parent/get or http://localhost:8080/parent/add
It can belong to request parameters and response data types
@ RequestMapping (value = "/ parent", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
Where consumes constraint input parameter type and produces constraint response data type
Test Content-Type:text/plain error due to JSON format
Which formats are supported to refer to the Media definition
Org.springframework.http.MediaTypeXML format data support
Here is an extension to return data in XML format
Introduce XML dependency package
Com.fasterxml.jackson.dataformat jackson-dataformat-xml
Test class
@ RestControllerpublic class DataRestContoller {@ RequestMapping (value = "/ addJsonResponseXml", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_XML_VALUE) public Map add (@ RequestBody Map data) {return data;}}
test
Thank you for reading this article carefully. I hope the article "sample Analysis of springboot RESTful and Parameter Notes" shared by the editor will be helpful to you. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!
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.