Java springboot project RESTful entry case understanding request type parameters

Let's create a java springboot project first
, and then let's try it in the old way. Create a class called UserController under
the directory controller at the same level as the startup class. The reference code is as follows

package com.example.threshold.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    
    

    @RequestMapping("/get")
    @GetMapping
    public String getMin(){
    
    
        return "访问成功";
    }
}

Then start the project
insert image description here
, the port assigned to me by the system is 8080

Then we use a browser to visit http://localhost:8080/user/get

insert image description here
You can see that this is successfully put back.
insert image description here
Change the second parameter of RequestMapping to method = RequestMethod, and the system will prompt you for all its types. Here we choose get.
insert image description here
The most commonly used four types should be get put post delete. Among them, get and There will be more post,
we will modify the reference code as follows

package com.example.threshold.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    
    

    @RequestMapping(value = "/get",method = RequestMethod.GET)
    @ResponseBody
    public String getMin(){
    
    
        return "访问成功";
    }
}

Restart the project,
insert image description here
the port has not changed, we visit http://localhost:8080/user/get again
without any problem

insert image description here
Then we modify the code as follows

package com.example.threshold.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    
    

    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String getMin(@PathVariable Integer id){
    
    
        return "您传输的id是"+id;
    }
}

PathVariable is telling the system that you need to get this parameter from the path, and you need to change the corresponding path. We added /id after the path. This id is the same as the parameter name id below, so it can match

We restart the program again and
visit
http://localhost:8080/user/get/3 This time our path is followed by a 3, which is the id we passed to it. The system also received it smoothly
insert image description here
and fed back our parameters

If your request parameters are to be obtained in the request header and are JSON, then just change PathVariable to RequestBody.
If it is a parameter after the path question mark, such as a request address? XX=XX or a form type parameter, then use RequestParam

Guess you like

Origin blog.csdn.net/weixin_45966674/article/details/130175609