There are multiple request parameters for the Springboot interface, including common type parameters and @ReuqestBody modified parameters. How to use Postman to call

1. Background introduction

Using a post interface developed by others, I found that there are multiple request parameters for the interface, and one of the parameters is modified with the @ReuqestBody annotation. For this kind of request parameter, I don’t know how to request it when using postman.

2. Introduction to the request methods of different Postman interfaces

2.1 Receiving Form data

2.1.1 Parameters are modified with @RequestParam annotation

Interface code:

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(@RequestParam("name") String name,
                        @RequestParam("age") Integer age) {
    
    
        return "name:" + name + "\nage:" + age;
    }
}

postman request:
Insert picture description here

2.1.2 No parameters

The interface has parameters, but the Controller will report an error if no parameters are passed. There are two solutions for this:

  • Use required = false to indicate that the parameter is not required
  • Use defaultValue to specify a default value for the parameter

Interface code:

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(@RequestParam(name = "name", defaultValue = "xxx") String name,
                        @RequestParam(name = "age", required = false) Integer age) {
    
    
        return "name:" + name + "\nage:" + age;
    }
}

postman request:
Insert picture description here

2.1.3 The request parameter is map type

Interface code:

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.Map;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(@RequestParam Map<String,Object> params) {
    
    
        return "name:" + params.get("name") + "\nage:" + params.get("age");
    }
}

postman request:
Insert picture description here

2.1.4 The request parameter is an array

When there are multiple parameters with the same name in the form, the Controller can define an array to receive the
interface code:

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.Map;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(@RequestParam("name") String[] names) {
    
    
        String result = "";
        for(String name:names){
    
    
            result += name + "\n";
        }
        return result;
    }
}

postman request:
Insert picture description here

2.1.5 The parameter is the object type

If a post request has too many parameters, we construct an object to simplify the parameter receiving
interface code:

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(User user) {
    
    
        return "name:" + user.getName() + "\nage:" + user.getAge();
    }
}

Postman request:
Insert picture description here
Note 1: If the passed parameter has a prefix and the prefix is ​​the same as the entity class name of the parameter in the interface, then the parameter can also be passed normally.
Insert picture description here
If the passed parameter has a prefix, and the prefix is ​​different from the name of the receiving entity class, then Parameters cannot be passed normally.
Insert picture description here
Note 2: If the parameters of a get request belong to different objects, multiple objects can also be used to receive the parameter
interface code:

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(User user, Phone phone) {
    
    
        return "name:" + user.getName() + "\nage:" + user.getAge()
                + "\nnumber:" + phone.getNumber();
    }
}

postman request:
Insert picture description here

2.2 Receiving string text data

2.2.1 Parameters make text type

If the text is passed, we can get the input stream through HttpServletRequest to read the text content

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(HttpServletRequest request) {
    
    
        ServletInputStream is = null;
        try {
    
    
            is = request.getInputStream();
            StringBuilder sb = new StringBuilder();
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = is.read(buf)) != -1) {
    
    
                sb.append(new String(buf, 0, len));
            }
            System.out.println(sb.toString());
            return "获取到的文本内容为:" + sb.toString();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            try {
    
    
                if (is != null) {
    
    
                    is.close();
                }
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
        return null;
    }
}

postman request:
Insert picture description here

2.3 Receiving JSON data

2.3.1 The request parameter is map type

If the parameters are passed in JSON form, we can use @Requestbody to receive the parameters and convert the data to the Map
interface code:

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.Map;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(@RequestBody Map params) {
    
    
        return "name:" + params.get("name") + "\n age:" + params.get("age");
    }
}

postman request:
Insert picture description here

2.3.2 Receiving object type parameters

Interface code:

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(@RequestBody User user){
    
    
        return user.getName() + " " + user.getAge();
    }
}

postman request:
Insert picture description here

2.3.3 Receiving array type parameters

If the passed JSON data is an array, it is possible to modify the interface code as follows
:

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(@RequestBody List<User> users){
    
    
        String result = "";
        for(User user:users){
    
    
            result += user.getName() + " " + user.getAge() + "\n";
        }
        return result;
    }
}

postman request:
Insert picture description here

Three, practice

After the second part of the study, I have some ideas about the third-party interface that I requested to call. The interface I want to request has a String type parameter decorated with @RequestParam annotation ; an int type parameter without any annotation decoration ; and a List parameter decorated with @RequestBody annotation

If you use postman request, there are two ways:

Method 1:
Combine the first two parameters into a url according to the get request method, such as: http://localhost:8080/api/test?param=sun¶m2=5, and the third request uses postman's json method, namely Can complete the call of the interface

Method 2:
Fill in the first two parameters in the form of postman, and the third parameter is still requested in json mode

Guess you like

Origin blog.csdn.net/sinat_34241861/article/details/114384117