1 what is RESTFul
-
RESTFul programming is a style, not a protocol.
-
Interpretation of HTTP protocol (landing scheme) and landing of request mode
-
There are 8 http protocols
Serial number | method | describe |
1 | GET | Request the specified page information. The request data is placed in the protocol header and the entity body is returned |
2 | POST | Submit resources to the server for processing, such as form data or picture data. The submitted data is placed in the request body. The content may be modified |
3 | PUT | Like POST, it is used to send resources to the server and store them in the specified location of the server. At the same time, PUT is secure. No matter how many requests are made, it is modified on the same resource |
4 | DELETE | Request the server to delete the specified resource or page. It is recommended to use https protocol, otherwise it may be blocked by firewall, |
5 | HEAD | Only the header information of the page is requested to check whether the chain solution can be used |
6 | OPTION | Allow the client to view the performance of the server and obtain the HTTP request method supported by the server; |
7 | TRACE | Echo the request received by the server and return part of the obtained content |
8 | CONNECT | Establish a network connection to the resource. After the connection is established, it will respond to a 200 status code and a "Connectioin Established" message. |
1.1 there are four commonly used http protocols
get
post
put
delete
RESTFul specifies the operation of the server program.
-
Each operation consists of: request path + request mode. (one path can complete different operations due to different request modes)
-
Data transmission mode: JSON data
// Traditional development path Query: http://localhost:8080/user/selectAll.action add to: http://localhost:8080/user/addUser.action Modification: http://localhost:8080/user/updateUser.action Delete: http://localhost:8080/user/deleteUser.action // RESTFul style path Query: get http://localhost:8080/user/ Details: get http://localhost:8080/user/123 add to: post http://localhost:8080/user/ Modification: put http://localhost:8080/user/ Delete: delete http://localhost:8080/user/123
2. Use
2.1 steps
-
Step 1: create a new project (mvc_restful)
-
Step 2: import jar packages: spring, spring mvc, jackson, mybatis
-
Step 3: configure the class,
-
spring mvc configuration
-
Start the configuration class. The front-end controller does not have an extension and is composed of * Change action to/
-
-
Step 4: write controller to complete addition, deletion, modification and query
4.1 class level:
@Controller ,@ResponseBody //-->Synthetic injection @RestController
4.2 method level: @ RequestMapping(value = "path", method=RequestMethod.GET /POST/PUT/DELETE)
//Traditional writing @RequestMapping(value="route", method=RequestMethod.GET /POST/PUT/DELETE) //-->Simplified writing of various request modes @GetMapping @PostMapping @PutMapping @DeleteMapping @PathVariable //Used to obtain path parameters
4.3 method return value
//According to the constraints and based on the RESTFul style, it is recommended to use the ResponseEntity type for the return value of the method // ResponseEntity is used to encapsulate return value information, including status code // Return 200 status code, responseentity OK ("added successfully"); // Other status codes, new responseentity < > ("added successfully", HttpStatus.UNAUTHORIZED)
3. Realization
3.1 configuration
package com.czxy.config; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.DispatcherServlet; import javax.servlet.FilterRegistration; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; public class WebInitializer1 implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { //1 configure spring Factory AnnotationConfigWebApplicationContext application = new AnnotationConfigWebApplicationContext(); // Register all configuration classes application.register(MvcConfiguration1.class); //2 post Chinese garbled code FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encoding", new CharacterEncodingFilter("UTF-8")); encodingFilter.addMappingForUrlPatterns(null, true, "/*"); //3 core controller ServletRegistration.Dynamic mvcServlet = servletContext.addServlet("springmvc", new DispatcherServlet(application)); mvcServlet.addMapping("/"); mvcServlet.setLoadOnStartup(2); //When tomcat starts, the initialization method of servlet is executed } }
3.2 controller
package com.czxy.controller; import com.czxy.domain.User; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.Date; import java.util.List; @RestController @RequestMapping("/user") public class UserController { /*Query all*/ @GetMapping public List<User> selectAll(){ ArrayList<User> list = new ArrayList<>(); list.add(new User("u001","jack","1234",new Date())); list.add(new User("u002","rose","4567")); list.add(new User("u003","jim","8888")); return list; } /*add to*/ @PostMapping public User save(User user){ System.out.println(user); return user; } // to update @PutMapping public String update(@RequestBody String id){ System.out.println(id); return "Default garbled code"; } /*Delete by id*/ @DeleteMapping("/{id}") public String delete(@PathVariable String id){ System.out.println(id); return "Default garbled code"; } /*Query details by id*/ @GetMapping("/{id}") public String findById(@PathVariable String id){ System.out.println(id); return "adopt id Query details"; } }