java Development SSM Framework SpringMVC with rest Style

  • 2021-11-24 01:45:15
  • OfStack

Directory RESTful Architecture Resource Presentation Layer (Representation) State Transition (State Transfer) Simple Summary SpringMVC Support for RESTful Architecture Complete Separation of Front and Back Ends Using ajax

RESTful architecture

REST (Resource Representational State Transfer) means "resource presentation layer state transition", and Resource (resource) is omitted. Clients are allowed to make requests to access and operate network resources with a unified resource identifier, which is coordinated with a predefined stateless operation set.

Resources

Refers to a specific information on the network, which can be text, pictures, videos, etc. Each resource corresponds to a specific URI. If you want to operate the resource to access its URI, you can, so URI becomes the address of every resource or the identifier of one resource without two.

Presentation layer (Representation)

The specific form of "resource" is called its "presentation layer" (Representation). For example, text can be expressed in txt format, HTML format, XML format, JSON format, and even binary format; Images can be presented in JPG format or PNG format. URI represents only the entity of the resource, not its form. Strictly speaking, the last ". html" suffix of some URLs is unnecessary, because this suffix indicates format and belongs to the category of "presentation layer", while URI should only represent the location of "resources". Its specific representation should be specified in the header information of the HTTP request with the fields Accept and Content-Type, which are the description of the "presentation layer."

State Transition (State Transfer)

One interaction process between client and server will involve changes in data and state. Internet communication protocol HTTP protocol, is a stateless protocol, all states are stored in the server. Therefore, if the client wants to operate the server, it must make the server-side "state transition" (State Transfer) by some means, which is based on the presentation layer, so it is called "presentation layer state transition". Because the technical means used by the client can only be HTTP protocol, the verbs of four operation modes: GET, POST, PUT and DELETE correspond to four basic operations respectively. GET is used to obtain resources, POST is used to create new resources (and can also be used to update resources), PUT is used to update resources, and DELETE is used to delete resources

A brief summary

Every URI represents one resource; Between the client and the server, some presentation layer of this resource is passed; The client uses four HTTP verbs to operate the server-side resources and realize "presentation layer state transformation". The software architecture conforming to REST specification is restful programming style.
In the style of Restful, the url requested by the user uses the same url and uses the request methods: get, post, delete, put, etc. to distinguish the processing methods of the request, so that the front-end developers will not confuse the requested resource address and have a lot of trouble in checking the method name in the development of separation between the front-end and the back-end, and form a unified interface

SpringMVC Support for RESTful Architecture

Spring MVC maps the request for resources into an URL by annotation, and represents and transmits resources through JSON and other formats; Use SpringMVC to accomplish

Need to get 1 parameter from URL So that different request methods of the same interface can complete corresponding operations

Design an RESTful interface to operate curriculum resources


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;

  	// Get all courses 
	@RequestMapping(value = "/course",method = RequestMethod.GET)
    public List<Course> getCourseList(){
        System.out.println("getCourseList");
        return service.selectCourses();
    }  
    // According to id Get a course 
    @RequestMapping(value = "/course/{id}",method = RequestMethod.GET)
    public Course getCourse(@PathVariable Integer id){
        System.out.println("getCourse");
        System.out.println(" Parameter :"+id);
        return service.selectByID(id);
    }
	  // Add a new course 
    @RequestMapping(value = "/course",method = RequestMethod.POST)
    public String addCourse(@RequestBody Course course){
        System.out.println("addCourse");
        System.out.println(" Parameter :"+course);
        service.insertCourse(course);
        return "{\"msg\":\"success\"}";
    }
		// According to id Delete a course 
    @RequestMapping(value = "/course/{id}",method = RequestMethod.DELETE)
    public String deleteCourse(@PathVariable Integer id){
        System.out.println("deleteCourse");
        System.out.println(" Parameter :"+id);
        service.deleteCourse(id);
        return "{\"msg\":\"success\"}";
    }
    // According to id Update the curriculum 
    @RequestMapping(value = "/course",method = RequestMethod.PUT)
    public String updateCourse(@RequestBody Course course){
        System.out.println("updateCourse");
        System.out.println(" Parameter :"+course);
        service.updateCourse(course);
        return "{\"msg\":\"success\"}";
    }
}

Using @ RestController as annotation in the code is equivalent to the combination of @ Controller + @ ResponseBody. It is not necessary to add @ ResponseBody annotation in front of the method to return json data, but using @ RestController annotation, jsp and html pages cannot be returned, and the view parser cannot parse jsp and html pages.

In a single-page project with completely separated front and back ends, routing jump 1 is generally carried out at the front end. At this time, it is easy to use (ajax or axios) to obtain data asynchronously, and at this time, it is easy to use @ RestController annotation.

Use http://in both RequestMapping and URL. . . /course, but method is different to realize adding, deleting, modifying and checking.

Note: In web application, only post and get can be used for form submission. put and delete cannot be submitted. You can configure the interceptor in web. xml
Sample code from: https://www.ofstack.com/article/128912. htm

Complete separation of front and back ends using ajax

At present, the popular single page system developed by web uses front-end routing, uses Ajax or AXOIS to complete the interactive communication between front-end and back-end, and generally uses json to complete the data transmission format. SpringMVC with rest programming style has become the mainstream technology of Java web development.

The above is the java development SSM framework with rest style SpringMVC details, more information about SpringMVC with rest style SSM framework please pay attention to other related articles on this site!


Related articles: