请求响应的概述
浏览器(请求)<--------------------------(HTTP协议)---------------------->(响应)Web服务器
请求:获取请求数据
响应:设置响应数据
BS架构:浏览器/服务器架构模式。客户端只需要浏览器,应用程序的逻辑和数据都存储在服务端(维护方便、体验一般)
CS架构:客户端/服务器架构模式(开发、维护麻烦、体验不错)
请求
postman
postman是一款功能强大的页面调试与发送网页HTTP请求的Chrome插件
作用:常用于进行接口测试
下载安装:
登录之后在Workspace中开始创建
调试:
简单参数:
原始方式:在原始的web程序中,获取请求参数,需要通过HTTP Servlet Request对象手动获取。
package com.bignyi.resp.controller;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";}
}
SpringBoot方式
简单参数:参数名与形参变量名相同,定义形参即可接收参数
package com.bignyi.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {@RequestMapping("/simpleParam")public String simpleParam(String name,Integer age){System.out.println(name+":"+age);return "OK";}
}
实体参数
简单实体对象:请求参数名与形参对象属性名相同,定义POJO接受即可
package com.bignyi.pojo;public class User {private String name;private int age;public int getAge() {return age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +", age=" + age +'}';}
}
package com.bignyi.controller;import com.bignyi.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//请求处理类
@RestController
public class HelloController {/* @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";}*/@RequestMapping("/simpleParam")public String simpleParam(String name,Integer age){System.out.println(name+":"+age);return "OK";}@RequestMapping("/simplePojo")public String simplePojo(User user){System.out.println(user);return "OK";}
}
复制实体对象:请求参数名与形参对象属性名相同,按照对象层次结构关系即可接收嵌套POJO属性参数。
package com.bignyi.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 + '\'' +'}';}
}
package com.bignyi.pojo;public class User {private String name;private int age;private Address address;public int getAge() {return age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public void setAge(int 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 +'}';}
}
package com.bignyi.controller;import com.bignyi.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//请求处理类
@RestController
public class HelloController {/* @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";}*/@RequestMapping("/simpleParam")public String simpleParam(String name,Integer age){System.out.println(name+":"+age);return "OK";}@RequestMapping("/simplePojo")public String simplePojo(User user){System.out.println(user);return "OK";}@RequestMapping("/complexPojo")public String complexPojo(User user){System.out.println(user);return "OK";}
}