Interface: API (Application Programming Interface) refers to some pre-defined interfaces (such as function and HTTP interface), or refers to the Convention for the connection of different components of the software system. It is used to provide a set of routines that can be accessed by applications and developers based on some software or hardware without accessing the source code or understanding the details of the internal working mechanism.
Interface (API): it can refer to accessing the url of servlet and controller and calling the functions of other programs
Architecture style: api organization
Is a traditional:
http://localhost:9002/mytrans/addStudent?name=lisi&age=26
The name of the accessed resource addStudent is provided on the address, and then the parameters are passed in the get mode.
1.1 understanding RESTful
1.1.1 RESTful architecture style
REST: (English: Representational State Transfer, Chinese: presentation layer state transfer).
REST: it is an interface architecture style and design concept, not a standard.
Advantages: more concise and hierarchical
Presentation layer state transition: the presentation layer is the view layer, which displays the of resources, and displays the results of operating resources through view pages, JSPS, etc.
Status: resource change
Transfer: resources can change. The resource can be created in new status. After the resource is created, you can query the resource and see the content of the resource,
The content of this resource can be modified. The modified resource is different from the previous one.
1.2 RESTful notes
The RESTful development of Spring Boot is mainly realized by several annotations
@PathVariable: get the data in the url.
This annotation is the most important annotation to realize RESTful
@GetMapping: receive requests in get mode, which is equivalent to @ requestmapping (method = requestmethod. Get).
@PostMapping: receiving and processing post mode requests, which is equivalent to @ requestmapping (method = requestmethod. Post).
@PutMapping: receive the request in put mode, which can be replaced by PostMapping, which is equivalent to @ requestmapping (method = requestmethod. Put).
@DeleteMapping: receive requests in the delete mode, which can be replaced by GetMapping, which is equivalent to @ requestmapping (method = requestmethod. Delete).
@RestController: conforms to the annotation. It is a combination of @ Controller and @ ResponseBody.
Use @ RestController on the top of the class to indicate that all methods of the current class have joined @ ResponseBody
1.3 use of restful style
1.3.1 add Maven dependency
<dependencies> <!--web Start dependence--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Test dependency--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
1.3.2 modify application Properties file
server.port=9001 server.servlet.context-path=/
1.3.3 writing Controller
package co.suyv.controller; import org.springframework.web.bind.annotation.*; @RestController public class MyRestController { /** * rest In, the url should use placeholders to represent the passed data. * Placeholders are called path variables, which are the data in the url * * Format: in the value attribute value of @ RequestMapping, use {custom name} * http://localhost:8080/myboot/student/1001/bj2009 * * @PathVariable: The path variable annotation is used to obtain the value of the path variable in the url * Attribute: value: path variable name * The position defined in the previous parameter, one by one * * Note: the path variable name is the same as the formal parameter name, and value can not be written * */ // Query students with id=1001 @GetMapping("/student/{studentId}") public String queryStudent(@PathVariable(value = "studentId") Integer id){ return "Query students studentId: " + id; } // Increase students @PostMapping("/student/{name}/{age}") public String creatStudent(@PathVariable("name") String name, @PathVariable("age") Integer age){ return "establish student: full name:"+ name + ",Age:" +age; } // Modify student @PutMapping("/student/{id}/{age}") public String modifyStudent(@PathVariable Integer id, @PathVariable Integer age){ return "modify student: id="+ "id" + ",age=" + age; } // Delete student @DeleteMapping("/student/{id}") public String removeStudentById(@PathVariable Integer id){ return "delete student: id=" + id; } }
1.3.4 query test
1.3.5 add test
1.3.6 modification test
1.3.7 delete test
1.4 RESTful benefits
1. Lightweight, directly based on http, no other message protocol is required: get/post/put/delete is CRUD operation
2. Resource oriented, clear at a glance and self explanatory.
3. The data description is simple. Generally, xml and json are used for data exchange.
4. Stateless. When calling an interface (accessing and operating resources), the context and current state can be ignored, which greatly reduces the complexity.
5. Simple and low coupling
1.5 RESTful summary
1.5.1 request path conflict
@GetMapping(value = "/student/{studentId}/{classname}") @GetMapping(value = "/student/{studentId}/{schoolname}")
Such path access will fail and there is a path conflict.
Solution: the design path must be unique, and the path uri and request method must be unique.
1.5.2 precautions
1. Add post request, delete request, change put request and query get request
2. There should be no verbs in the request path, for example: query order interface
/boot/order/1021/1 (recommended)
/boot/queryOrder/1021/1 (not recommended)
3. Paging, sorting and other operations do not need to use slash to transfer parameters, such as order list interface
/boot/orders? Page = 1 & sort = desc generally, the parameter passed is not a field in the database table, and the slash can not be used