Web请求与响应
目录
Postman
Postman简介?
postman是一款功能强大的网页调试与发送网页http请求的Chrome插件,常用于进行接口测试。
Postman的使用?
先创建用户?
?之后通过刚刚创建的用户登录
创建工作空间
?
?创建完成后他会自动进入到springbootweb工作空间
点击+添加新请求
请求
?pom.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.he1220</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
简单参数?
以原始的方式接收请求参数?
?目录结构如下
RequestController.java文件内容如下
package com.he1220.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
public class RequestController {
@RequestMapping("/simpleParam")
public String simpleParam(HttpServletRequest request){
String name = request.getParameter("name");
String age = request.getParameter("age");
System.out.println(name);
System.out.println(age);
return "ok";
}
}
测试结果如下
?以SpringBoot的方式接收请求参数
package com.he1220.demo.controller;
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){
System.out.println(name);
System.out.println(age);
return "ok";
}
}
如果方法形参名称与请求参数名称不匹配,可以通过@RequestParam注解完成映射
通过name属性指定请求参数名
@RequestParam中的Required属性默认为true,该请求参数必须传递,不传递将报错(400)。可以设置为false
package com.he1220.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RequestController {
@RequestMapping("/simpleParam")
public String simpleParam(@RequestParam(name="name")String username,Integer age){
System.out.println(username);
System.out.println(age);
return "ok";
}
}
?实体参数
需要保证请求参数名与形参对象属性名一致
?目录结构如下
RequestController.java内容如下?
package com.he1220.demo.controller;
import com.he1220.demo.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RequestController {
@RequestMapping("/Pojo")
public String Pojo(User user){
System.out.println(user);
return "ok~";
}
}
User.java内容如下
package com.he1220.demo.pojo;
public class User{
private String name;
private String age;
private Add add;
public User(String name, String age) {
this.name = name;
this.age = age;
}
public Add getAdd() {
return add;
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAdd(Add add) {
this.add = add;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", add=" + add +
'}';
}
}
?Add.java内容如下
package com.he1220.demo.pojo;
public class Add {
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 "Add{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
'}';
}
}
测试结果如下
?数组参数
?RequestController.java内容如下?
package com.he1220.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
@RestController
public class RequestController {
@RequestMapping("/arrParam")
public String arrParam(String[] hobby){
System.out.println(Arrays.toString(hobby));
return "ok~";
}
}
测试结果如下
?
?集合参数
需要保证请求的参数名与集合变量名相同,通过@RequestParam注解绑定参数关系
?RequestController.java内容如下??
package com.he1220.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class RequestController {
@RequestMapping("/listParam")
public String listParam(@RequestParam List<String> hobby){
System.out.println(hobby);
return "ok~";
}
}
?日期参数
使用@DateTimeFormat 注解完成日期参数格式转换?
package com.he1220.demo.controller;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
@RestController
public class RequestController {
@RequestMapping("/dateParam")
public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime){
System.out.println(updateTime);
return "ok~";
}
Json参数
通过@RequestBody注解将Json对象封装到实体类当中?
package com.he1220.demo.controller;
import com.he1220.demo.pojo.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RequestController {
@RequestMapping("/jsonParam")
public String jsonParam(@RequestBody User user){
System.out.println(user);
return "ok~";
}
}
路径参数?
?通过请求URL直接传递参数,使用{...}来标识该参数路径,需要使用@PathVariable获取参数路径
@RestController
public class RequestController {
@RequestMapping("/path/{id}/{name}")
public String pathParam(@PathVariable Integer id,@PathVariable String name){
System.out.println(id + name);
return "ok~";
}
}
?响应
@ResponseBody?
放在Controller方法上/类上
将方法返回值直接响应,如果返回值类型是实体对象/集合,将会转换为JSON格式响应
@RestController = @Controller + @ResponseBody
统一响应结果
目录结构如下
?pojo.Result.java文件内容如下?
package com.he1220.demo.pojo;
/**
* 统一响应结果封装类
*/
public class Result {
private Integer code ;//1 成功 , 0 失败
private String msg; //提示信息
private Object data; //数据 data
public Result() {
}
public Result(Integer code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public static Result success(Object data){
return new Result(1, "success", data);
}
public static Result success(){
return new Result(1, "success", null);
}
public static Result error(String msg){
return new Result(0, msg, null);
}
@Override
public String toString() {
return "Result{" +
"code=" + code +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
?RequestController.class内容如下??
package com.he1220.demo.controller;
import com.he1220.demo.pojo.Add;
import com.he1220.demo.pojo.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RequestController {
@RequestMapping("/getAddr")
public Result getAddr(String province, String city){
System.out.println(province);
System.out.println(city);
Add addr = new Add();
addr.setProvince(province);
addr.setCity(city);
return Result.success(addr);
}
}
测试结果如下
???????
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!