Postman+Java springboot demonstrates get post put delete request and carries (json form after path path question mark) parameter form

Let's first create a java springboot project.
Find the location directory of the startup class in the project.
insert image description here
Create a class called user in the project.
I want to use user as an attribute class. According to the specification, we can create an entity package and then create attributes below. class, but we don’t want to make such a hassle here, after all, it’s just an exercise
, and the user reference code is as follows

package com.example.threshold;

public class user {
    
    
    private String name;
    public String getName(){
    
    
        return this.name;
    }
    public void setName(){
    
    
        this.name = name;
    }
}

Here we just define a name attribute string type
and declare it. After all, the basic get and set functions,
this class is used to receive and return json data

Then we create a package called controller under the startup class directory
and create a class called UserController under the controller.
insert image description here
The reference code of the UserController class is as follows

package com.example.threshold.controller;

import com.example.threshold.user;
import org.springframework.web.bind.annotation.*;

//声明RestController控制器
@RestController
//设置本类的公共前缀user
@RequestMapping("/user")
public class UserController {
    
    

    // 设置一个 GET接口  子路径get  接受一个id参数  id要跟在路径后面
    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String getId(@PathVariable Integer id) {
    
    
        return "您要获取的用户id为"+id;
    }

    //设置一个 POST请求  子路径叫  add 接受一个name参数   需要传递的方式是一个 表单传参或者路径问号后传参方式带的参数
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public String addUser(@RequestParam String name) {
    
    
        return "你要添加的用户名称为"+name;
    }

    //设置一个 PUT请求 子路径叫  edit   接受一个user类对象参数  要求json传参
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @ResponseBody
    public user editUser(@RequestBody user user) {
    
    
        //把接到的json传回去
        return user;
    }

    // 设置一个DELETE请求  子路径为delete   接受一个参数id 和 一个type参数  接受方式为  表单或者 路径问号后携带参数
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public String deleteUser(@RequestParam Integer id,Integer type) {
    
    
        return "你要删除的用户id为"+id+"操作类型为"+type;
    }
}

Here we set the four most commonly used request methods for development:
get, get data,
post, add/submit data,
put, modify data,
delete, delete data,
and then use the path to carry the path question mark and then carry the json form. Four more common ways of passing parameters

Then we open Postman
to create a project
insert image description here
, and then we first test the first get path with parameters.
First, we need to start the project.
insert image description here
From the information output by the system below, we can see that the program is running on port 8080.
insert image description here
Here we want to request this class by requesting the port http:/ /localhost:8080/
But later, because our class declares the path user to request the interface of this class, we need to add user http://localhost:8080/user
after the port and then add the method path we want to request is http:/ Add a path id after /localhost:8080/user/get , then we go back to Postman to create a request under the right-click project, name the request and put the path, here the id after the path obviously gives 123321 to choose the request type as get, and the name is also Enter the data obtained by id, and send the request directly when it is done. Here, our first one obviously works.






insert image description here

insert image description here



insert image description here

Then try the second post form to pass parameters
, or create a request in the Postman project.
insert image description here
Here, request a name
, and then select the post
path for the request method and put it. The path above the method name is add
, so it is http://localhost:8080/user /add
and then select the request method of Body.
The parameter type selection form is
the key-value pair position under form-data to enter our key and value.
Here our key is name and the value is kitten.
insert image description here
After finishing it, click to run
insert image description here
, so that we can pass parameters in the form post request is also ok

Then let's try a highlight
put that accepts json parameters and returns a json request.
The request path is edit
, so it is http://localhost:8080/user/edit
Here we still create a request on the Postman project
insert image description here
and modify its name path request The method is set to put
and the parameter is still selected as body
, but the next step is to select raw.
There are many types of raw. Here we need to select request json
insert image description here
and then enter our json

{
    
    
    "name":"小猫猫"
}

Run it when you are done
insert image description here
, you can see that not only the returned json is OK

Then let's try the last DELETE request path and pass
the parameter after the question mark. The method declares the path delete, so it is
http://localhost:8080/user/delete?id= KaTeX parse error: Expected 'EOF', got '&' at position 5 : {id}&̲type= {type}

Here we still create a request on the Postman project.
insert image description here
Here we only need to put the path because you have a parameter after the question mark. Postman is smarter and it will automatically identify it for you. Then
we select the request type as delete
insert image description here
and we send the request.
insert image description here
The process was also very smooth

Guess you like

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