Postman接口测试工具,提高SpringBoot开发效率

文章目录

🌺工具---postman

postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件

⭐作用

常用于进行接口测试

🏳️‍🌈安装

我把安装包传到网盘里面了,点击exe文件就可以下载,需要的同学请自取

链接

🎈创建工作空间



点击CTRL+S进行保存

这样子即使我们关了,也能够访问

🎄简单参数

⭐原始方式

在原始的web程序中,获取请求参数,需要通过HttpServletRequest对象手动获取

🎈我们建立springboot项目,输入下面的代码

java 复制代码
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class RequestController  {
    //原始方法
    //设置注解,指定请求路径
    @RequestMapping("/simpleParam")
    public String simpleParam(HttpServletRequest request){
        //获取请求参数
        String name=request.getParameter("name");
        String ageStr=request.getParameter("age");

        int age=Integer.parseInt(ageStr);
        System.out.println(name+":"+age);
        return "OK";
    }
}

🎈运行

这时候我们打开postman(不要终止项目运行)

输入http://localhost:8080/simpleParam?*name=Tom* &age=10

然后点击send

⭐SpringBoot方式

只需要保证参数名和变量名相同,定义形参即可接收参数

java 复制代码
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class RequestController  {
    //原始方法
    //设置注解,指定请求路径
    @RequestMapping("/simpleParam")
    public String simpleParam(String name,Integer age){
        //SpringBoot方式
        System.out.println(name+":"+age);
        return "OK";
    }
}

🎄实体参数

🏳️‍🌈简单实体参数

请求参数名与形参对象属性名相同,定义POJO接收即可

⭐代码实现

创建类User.java

java 复制代码
public class User {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

创建类RequestController.Java

java 复制代码
import com.example.springbootwebreqresp.pojo.User;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class RequestController  {

    //实体参数
    //设置注解,指定请求路径
    @RequestMapping("/simplePojo")
    public String simplePojo(User user){
        System.out.println(user);
        return "OK";
    }
}

🏳️‍🌈复杂实体参数

请求参数名与形参对象属性名相同,安装对象层次结构关系即可接收嵌套POJO属性参数

⭐代码实现

创建类User.java

java 复制代码
package com.example.springbootwebreqresp.pojo;

public class User {
    private String name;
    private Integer age;

    private Address address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }
}

创建类Address.java

java 复制代码
package com.example.springbootwebreqresp.pojo;

public class Address {
    private String province;
    private String city;

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Address{" +
                "province='" + province + '\'' +
                ", city='" + city + '\'' +
                '}';
    }
}

创建类RequestController.Java

java 复制代码
package com.example.springbootwebreqresp.controller;

import com.example.springbootwebreqresp.pojo.User;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


    @RequestMapping("/complexPojo")
    public String complexPojo(User user){
        System.out.println(user);
        return "OK";
    }
}

🎄数组集合参数

⭐使用数组来接收

请求参数名与形参数组名相同且请求参数为多个,定义数组类型形参即可接收参数

java 复制代码
package com.example.springbootwebreqresp.controller;

import com.example.springbootwebreqresp.pojo.User;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;
import java.util.Arrays;


@RestController
public class RequestController  {
    @RequestMapping("/arrayParam")
    public String arrayParam(String[] hobby){
        System.out.println(Arrays.toString(hobby));
        return "OK";
    }
}

⭐使用集合来接收

请求参数名与形参集合名称相同且请求参数为多个,@RequestParam绑定参数关系

java 复制代码
package com.example.springbootwebreqresp.controller;

import com.example.springbootwebreqresp.pojo.User;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;


@RestController
public class RequestController  {
    @RequestMapping("/arrayParam")
    public String arrayParam(@RequestParam List<String>hobby){
        System.out.println(hobby);
        return "OK";
    }
}

🎆小结

🎄日期参数

使用@DateTimeFormat注解完成日期参数格式转换

🎄路径参数

🥰总结


相关推荐
qq_124987075339 分钟前
基于Java Web的城市花园小区维修管理系统的设计与实现(源码+论文+部署+安装)
java·开发语言·前端·spring boot·spring·毕业设计·计算机毕业设计
Luminbox紫创测控41 分钟前
整车自然暴晒与全光谱阳光模拟老化相关性研究
测试工具
VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vue云租车平台系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
Chasmれ1 小时前
Spring Boot 1.x(基于Spring 4)中使用Java 8实现Token
java·spring boot·spring
汤姆yu1 小时前
2026基于springboot的在线招聘系统
java·spring boot·后端
计算机学姐1 小时前
基于SpringBoot的校园社团管理系统
java·vue.js·spring boot·后端·spring·信息可视化·推荐算法
Coder_Boy_2 小时前
基于SpringAI的在线考试系统-企业级教育考试系统核心架构(完善版)
开发语言·人工智能·spring boot·python·架构·领域驱动
java1234_小锋2 小时前
Java高频面试题:SpringBoot如何自定义Starter?
java·spring boot·面试
indexsunny2 小时前
互联网大厂Java面试实战:微服务与Spring生态技术解析
java·spring boot·redis·kafka·mybatis·hibernate·microservices
Warren983 小时前
Pytest Fixture 作用域与接口测试 Token 污染问题实战解析
功能测试·面试·单元测试·集成测试·pytest·postman·模块测试