做电影网站还能赚钱,wordpress图集插件,互联网站管理工作细则,个人博客网站需求分析文章目录 #x1f33a;工具—postman⭐作用#x1f3f3;️#x1f308;安装#x1f388;创建工作空间 #x1f384;简单参数⭐原始方式#x1f388;我们建立springboot项目#xff0c;输入下面的代码#x1f388;运行 ⭐SpringBoot方式 #x1f384;实体参数#x… 文章目录 工具—postman⭐作用️安装创建工作空间 简单参数⭐原始方式我们建立springboot项目输入下面的代码运行 ⭐SpringBoot方式 实体参数️简单实体参数⭐代码实现 ️复杂实体参数⭐代码实现 数组集合参数⭐使用数组来接收⭐使用集合来接收小结 日期参数路径参数总结 工具—postman
postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件
⭐作用
常用于进行接口测试
️安装
我把安装包传到网盘里面了点击exe文件就可以下载需要的同学请自取 链接 创建工作空间 点击CTRLS进行保存 这样子即使我们关了也能够访问
简单参数
⭐原始方式
在原始的web程序中获取请求参数需要通过HttpServletRequest对象手动获取
我们建立springboot项目输入下面的代码 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 namerequest.getParameter(name);String ageStrrequest.getParameter(age);int ageInteger.parseInt(ageStr);System.out.println(name:age);return OK;}
} 运行
这时候我们打开postman不要终止项目运行 输入http://localhost:8080/simpleParam?nameTomage10 然后点击send
⭐SpringBoot方式
只需要保证参数名和变量名相同定义形参即可接收参数
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 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;}Overridepublic String toString() {return User{ name name \ , age age };}
}
创建类RequestController.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
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;}Overridepublic String toString() {return User{ name name \ , age age , address address };}
}
创建类Address.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;}Overridepublic String toString() {return Address{ province province \ , city city \ };}
}
创建类RequestController.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;}
} 数组集合参数
⭐使用数组来接收
请求参数名与形参数组名相同且请求参数为多个定义数组类型形参即可接收参数
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绑定参数关系
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 ListStringhobby){System.out.println(hobby);return OK;}
} 小结 日期参数
使用DateTimeFormat注解完成日期参数格式转换
路径参数 总结