Springboot接口的请求参数有多个,包括普通类型参数和@ReuqestBody修饰的参数,如何使用Postman调用

一、背景介绍

使用别人开发的一个post接口,发现该接口请求参数有多个,并且其中一个参数是用@ReuqestBody注解修饰的。对于这种请求参数,使用postman时不知如何请求

二、Postman不同接口的请求方式介绍

2.1 接收Form表单数据

2.1.1 参数使用@RequestParam注解修饰

接口代码:

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请求:
在这里插入图片描述

2.1.2 不传参数

接口有参数,但是如果没有传递参数时 Controller 将会报错,这个有如下两种解决办法:

  • 使用 required = false 标注参数是非必须的
  • 使用 defaultValue 给参数指定个默认值

接口代码:

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请求:
在这里插入图片描述

2.1.3 请求参数为map类型

接口代码:

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请求:
在这里插入图片描述

2.1.4 请求参数为数组

当表单中有多个同名参数时,Controller 可以定义一个数组进行接收
接口代码:

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请求:
在这里插入图片描述

2.1.5 参数为对象类型

如果一个 post 请求的参数太多,我们构造一个对象来简化参数的接收
接口代码:

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请求:
在这里插入图片描述
注意1:如果传递的参数有前缀,且前缀与接口中参数的实体类名称相同,那么参数也是可以正常传递的
在这里插入图片描述
如果传递的参数有前缀,且前缀与接收实体类的名称不同,那么参数无法正常传递
在这里插入图片描述
注意2:如果一个 get 请求的参数分属不同的对象,也可以使用多个对象来接收参数
接口代码:

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请求:
在这里插入图片描述

2.2 接收字符串文本数据

2.2.1 参数使文本类型

如果传递过来的是 Text 文本,我们可以通过 HttpServletRequest 获取输入流从而读取文本内容

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请求:
在这里插入图片描述

2.3 接收JSON数据

2.3.1 请求参数是map类型

如果以JSON形式传递参数,我们可以使用 @Requestbody 接收参数,将数据转换 Map
接口代码:

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请求:
在这里插入图片描述

2.3.2 接收对象类型参数

接口代码:

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请求:
在这里插入图片描述

2.3.3 接收数组类型参数

如果传递的 JSON 数据是一个数组也是可以的,Controller 做如下修改
接口代码:

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请求:
在这里插入图片描述

三、实践

经过第二部分的学习,对于请求我调用的第三方接口有了一些思路。我要请求的接口中有@RequestParam注解修饰的String类型参数一个没有任何注解修饰的int类型参数还有一个用@RequestBody注解修饰的List参数

如果使用postman请求的话,有两种方式:

方式一:
将前两个参数按照get请求的方式拼成一个url,如:http://localhost:8080/api/test?param=sun&param2=5,第三个请求用postman的json方式请求,即可完成接口的调用

方式二:
将前两个参数按postman的form表单方式填写参数,第三个参数仍然按json方式请求

猜你喜欢

转载自blog.csdn.net/sinat_34241861/article/details/114384117
今日推荐