A, REST brief
REST represents the Representational State Transfer, is a URI style, is a set of architectural constraints and principles. REST-style URL request is to parse the service call, the request from the processing logic building, and the results returned to the calling form the front end of one kind. This style is widely used in the interaction between the micro-service system, typically separated front and rear ends projects using this style, not a standard. To summarize the design specifications rest interface name represented by the URL, HTTP request method to distinguish the type of the request, while the front end status response according to HTTP operations accordingly
Two, URI introduction
URI: Uniform Resource Locator (Uniform Resource Locator), resources (Resource) is an entity on the network, or is a specific information on the network, each resource category, and its resource entities can be uniquely identified by a URI address such https://cnblogs.com identification blog resource, https: //cnblogs.com/p/2478932.html represents a unique entity of blog resources, its resource is cnblogs.com, resource entity represents is 2,478,932
Dui resource and resource access entities referred to as resource request and the operation (ResourceRequest), comprising a request to access the resource class, resource entity (optional) and the resource operation. As described above, resource, and the resource may be represented by a single entity of the URI, and the operation of resource (i.e. resource CRUD) is required by the method (Method) and parameters (the Param) FIG.
Examples of resources based on a request
method | URL | Example Explanation |
GET | https://cnblogs.com/ | Get blog class for all entities |
GET | https://cnblogs.com/23424 | Gets the blog article resource entities |
POST | https://cnblogs.com/ | Create a blog post, the parameter can be an object that contains the initial value |
PUT | https://cnblogs.com/23424 | Updates the specified resource entity blog post, the parameters are subject to change increments |
DELETE | https://cnblogs.com/23424 | Delete the specified resource instance |
The concept of resources across all types of background and front REST-style service between the two ends in the form of exposed, so that each may correspond to a resource URI the HTTP URL, the resource request and the method correspond exactly HTTPMethod.
In the background, first need to define the operating resources, in accordance with the agreed method of operating a resource URL request address information of the path, the path in the URL request and a parameter corresponding to the HTTP request method, is mapped to a specific resource class and operation methods.
HTTP exist to create, modify, access, delete these types of operations
Rest style url generally not a verb, the operation mode of the resource has been handed over to HTTPMethod the
error: http: xxxxxx.com/getUserById
Before and after contrast unused RESTful
operating | Unused RESTful | Use RESTful |
Find (SELECT-GET) | /rest/api/getUser | /rest/api/user |
Modifying (UPDATE-PUT) |
/rest/api/editUser | /rest/api/user/34543 |
Increase (INSERT-POST) | /rest/api/addUser | /rest/api/user/34543 |
Delete (DELETE-PUT) | /rest/api/deleteUser | /rest/api/user/34543 |
Three, SpringBoot REST-style request to call
Based SpringBoot below using exemplary request rest Style:
@RestController @RequestMapping(value="/rest/api") public class ConsTranChkController { @Autowired private IConsTranChkService consTranChkService; /** * RESTful查询 * @return * @author liufq * @Date 2019年11月14日 下午5:44:41 */ @RequestMapping(value="/user", method=RequestMethod.GET) public List<Map<String, Object>> getUserInfo(){ List<Map<String, Object>> resultList = consTranChkService.getChksList(); System.out.println(resultList); return resultList; } /** * RESTful修改 * @param userId * @return * @author liufq * @Date 2019年11月14日 下午5:44:56 */ @RequestMapping(value="user/{userId}", method=RequestMethod.PUT) public int editUser(@PathVariable String userId){ System.out.println("获取到用户ID:" + userId); return consTranChkService.editUser(userId); }
This is no longer attached to the code layer and the service layer Dao, just briefly described request from the control layer to the front end of the spring receiving rest style parameters, to more clearly see HTTPMethod, we can use
@RequestMapping (value = "/ user", method = RequestMethod.GET) such as this comment form, so that URL mapped to the corresponding controller, but in order to better support REST-style development,
After the spring is also provided @ GetMapping, @ PostMapping, @ PatchMapping , @ DeleteMapping
may be positioned on the controller designated by the method @ RequestMapping, @ GetMapping other annotation, annotation can be obtained by @Pathvariable URL parameters, can talk request by @RequestBody json format to a character string into a java object.