SpringMVC develop RESTful interface

concept:

What is REST?

REST is Representational State Transfer acronym. Translated as "presentation layer state transformation", restful is an interface design style, it is not an agreement, usually based on the HTTP protocol;

Why do we need such a style?

One of the key RESTful interface is unified naming rules;

Each developer may have a different interface styles, the most common is similar to getUserInfo, deleteUserInfo etc ....., but this is purely to do with each developer habits, it could multiplayer collaborative development cause problems, especially when Taiwan before and after the separation, the front desk staff had to fill in a lot of different url to request data;

RESTful style:

The rest are each seen as a resource URI, which is a concept that can actually be a picture, a record, a record can be set; each corresponds to the operation request method on a resource, typically comprising four categories:

  • GET access to resources
  • PUT-renewable resources
  • POST submit resources
  • DELETE Delete Resource

We will assume that the user id 1 data is seen as a resource, then we will be operating at the front desk to send a request to locate the address of the server resources to this resource, such as: http: // localhost: 8080 / SSMDemo / user / 1, after finding resources by URI, we have to tell the server we want this resource what kind of operation, it is through HTTP request methods; such as GET

Simply put: a RESTful URI is used to locate the resource, defined in the requests to be performed by a method of operation;

For now, in full accordance with RESTful design of the site is not much, Amazon regarded as one of the first use of this style of website, its URL is like this:https://www.amazon.cn/gp/product/B00MCW8R1S

RESTful of statelessness:

Stateless constraints prevent changes in the server to the client is not visible, two consecutive requests, the client does not depend on the same server, which makes the server has better scalability;

RESTful system to bring the benefits of:

Reduce the complexity of development, improve the scalability of the system, so that more standardized interfaces;

URI given URL:

URL is a uniform resource identifier (as long as it uniquely identifies a resource called URI)

A URL is a Uniform Resource Path

URL is a form of URI

Of RESTful SpringMVC

Can be seen, the change in RESTful that request processing of the address, and the request method is defined;

We have two things to do:

  • We need to get some parameters from the URL
  • Such that the same interface to request different ways to accomplish the operation corresponding

Case: design a RESTful interface to operate on curriculum resources

Write controller

package com.kkb.controller;

import com.kkb.pojo.Course;
import com.kkb.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class RESTCourseController {

    @Autowired
    CourseService service;

    //获取全部课程
        @RequestMapping(value = "/course",method = RequestMethod.GET)
    public List<Course> getCourseList(){
        System.out.println("getCourseList");
        return service.selectCourses();
    }  
    //根据id获取某个课程
    @RequestMapping(value = "/course/{id}",method = RequestMethod.GET)
    public Course getCourse(@PathVariable Integer id){
        System.out.println("getCourse");
        System.out.println("参数:"+id);
        return service.selectByID(id);
    }
      //添加新的课程
    @RequestMapping(value = "/course",method = RequestMethod.POST)
    public String addCourse(@RequestBody Course course){
        System.out.println("addCourse");
        System.out.println("参数:"+course);
        service.insertCourse(course);
        return "{\"msg\":\"success\"}";
    }
        //根据id删除课程
    @RequestMapping(value = "/course/{id}",method = RequestMethod.DELETE)
    public String deleteCourse(@PathVariable Integer id){
        System.out.println("deleteCourse");
        System.out.println("参数:"+id);
        service.deleteCourse(id);
        return "{\"msg\":\"success\"}";
    }
    //根据id更新课程
    @RequestMapping(value = "/course",method = RequestMethod.PUT)
    public String updateCourse(@RequestBody Course course){
        System.out.println("updateCourse");
        System.out.println("参数:"+course);
        service.updateCourse(course);
        return "{\"msg\":\"success\"}";
    }
}

@PathVariable is designed for obtaining parameters from the url, adding RequestMapping parameter name in {}, as a placeholder for the parameter name, parameter names and method require the same, if a different value may be added to @PathVariable (generally not necessary to do so), like this:

  @RequestMapping(value = "/course/{cid}",method = RequestMethod.GET)
    public Course getCourse(@PathVariable("cid") Integer id){
        System.out.println("getCourse");
        System.out.println("参数:"+id);
        return service.selectByID(id);
    }

Interface testing is recommended postman mac can use paw,

Also in the actual development, we need to pass the front desk user token to authenticate; can be achieved by interceptor;

supplement:

Tomcat will only get and post analytical parameters in, if SpringMVC used in PUT or DELETE, and using a form is submitted, it will not be available parameters, Tomcat should not continue through out the parameters put in the request, provided SpringMVC a filter designed to help tomcat resolution parameters put / delete and placed in the request, the configuration is as follows:

web.xml:

<!--配置SpringMVC,解析PUT/DELETE的表单数据放入request-->
    <filter>
        <filter-name>HttpPutFormContentFilter</filter-name>
        <filter-class>org.springframework.web.filter.FormContentFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HttpPutFormContentFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

The above-described problem does not occur when using json interaction, because SpringMVC json data is acquired directly from the request body, not by the Request;

Winded: If the form on the page using the PUT / DELETE request or mode, it needs to be added above the filter in web.xml;

Guess you like

Origin www.cnblogs.com/yangyuanhu/p/12304791.html