Spring MVC on the initial use of RESTFUL style

RESTFUL Style Introduction

restful is a software architecture style and design style, not a standard, but provides a set of design principles and constraints. It is mainly used for client and server interaction software. The software designed based on this style can be more concise, more hierarchical, and easier to implement caching and other mechanisms
The key to RESTful is to define objects that can represent process elements / resources. In REST, each object is represented by a URL. The object user is responsible for packaging the state information into each message so that the processing of the object is always stateless.

RESTFUL refers to the specific operation type of resources, which is represented by HTTP verbs.
There are five commonly used HTTP verbs:

1.GET (SELECT): fetch resources (one or more items) from the server
HTTP GET /user/1: get user with id=1
HTTP GET /user: get all user information
2.POST (CREATE): CREATE a new resource on the server.
HTTP POST /user: add a new user. The submitted user information needs to be submitted in the body of the request
3.PUT (UPDATE): UPDATE resources on the server (the client provides the changed complete resources).
HTTP PUT /user/1: modify the user information with id=1. You need to submit the modified user information in the body of the request
4.PATCH (UPDATE): UPDATE resources on the server (the client provides changed attributes).
HTTP PATCH /user/1: modify the user information with id=1. You need to submit the modified user information in the body of the request
5.DELETE (DELETE): deletes resources from the server.
HTTP DELETE /user/1: delete user with id=1

Of course, it can be understood as defining requests. One request performs one type of operation, and uses requests to distinguish. The difference is that we distinguish operations according to requests

Spring MVC code implementation

1. Implementation of get request (definition of get request: get data)

JQ request processing:

$("#btn1").click(function (){
    var id=9;
    $.get("${sm}/selectUser/"+id,{},function (data){
         console.log(data);
    });
});

java backend processing:

    @RequestMapping(value = "/selectUser/{id}",method = RequestMethod.GET)
    @ResponseBody
    public User selectUser(@PathVariable(value = "id",required = true) int userId){
        //Obtain data (since the database is not connected, it is good to query the data from the database)
        User user=new User();
        user.setUserName("Xiao Liu");
        user.setPassword("666");
        return user;
    }

2. Implementation of post request (definition of post request: new data)

JQ request processing:

$("#btn2").click(function (){
    $.post("${sm}/insertUser",{
         userName:"1111",
         password:"55"
     },function (data){
  		console.log(data);
  	});
});

java backend processing:

	 @RequestMapping(value = "/insertUser",method = RequestMethod.POST)
	 @ResponseBody
	 public Boolean insertUser(User user){
	     //New data (since the database is not connected, it is good to supplement the new data here)
	     return false;
	 }

3. Implementation of put request (definition of put request: modify (update) data)

JQ request processing:

$("#btn3").click(function (){
	//Data to modify
  	var data={
     userName:"1111",
     password:"55"
    };
    //id
   var id=8;
   $.ajax({
       type:"PUT",
       url:"${sm}/updateUser/"+id,
       data:data,
       success:function (data){
           console.log(data);
       }
   });
});

java backend processing:

	 @RequestMapping(value = "/updateUser",method = RequestMethod.PUT)
	 @ResponseBody
	 public Boolean updateUser(@PathVariable(value = "id",required = true) int userId,User user){
	     //Update data (since the database is not connected, it is good to supplement the modification result of the data here)
	     return false;
	 }

4. Implementation of delete request (definition of delete request: delete data)

JQ request processing:

var id=9;
$("#bt4").click(function (){
    $.post("${sm}/deleteUser/"+id,{},function (data){
  		console.log(data);
  	});
});

java backend processing:

	 @RequestMapping(value = "/deleteUser",method = RequestMethod.DELETE)
	 @ResponseBody
	 public Boolean deleteUser(@PathVariable(value = "id",required = true) int userId){
	     //Update data (since the database is not connected, it is good to supplement the modification result of the data here)
	     return false;
	 }

It can be found that the background distinguishes operations by the request type and the requested path. Because of this, whether the corresponding parameter position is very important. Because it is not in the form of name:value, when taking parameters from the path, they are matched according to the position. patch is not mentioned, but the use method is basically the same. Most of them are put

At present, it is also a novice of spring MVC. I just got a clue. If there is a problem, please correct it

Keywords: Java

Added by aa720 on Mon, 10 Jan 2022 20:44:02 +0200