1.请求响应
1.安装postman
2.简单的参数
//原始的请求参数的方法
@RequestMapping("/simoleParam")
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请求参数
@RequestMapping("/simpleParam")
public String simpleParam(String name,Integer age){
System.out.println(name+":"+age);
return "ok";
}
/*@RequestParam(name = "name",required = false)
加上注解以后,前段参数可以与接收参数名字不一致
required默认为true 表示参数不能为空*/
@RequestMapping("/simpleParam")
public String simpleParam(@RequestParam(name = "name",required = false) String username, Integer age){
System.out.println(username+":"+age);
return "ok";
}
2.实体参数
@RequestMapping("/complexPojo")
public String simpleJoJo(User user){
System.out.println(user);
return "获取参数~";
}
package com.wwl.jojo;
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 +
'}';
}
}
package com.wwl.jojo;
public class Address {
private String provice;
public String city;
public String getProvice() {
return provice;
}
public void setProvice(String provice) {
this.provice = provice;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address{" +
"provice='" + provice + '\'' +
", city='" + city + '\'' +
'}';
}
}
3.数组集合参数
@RequestMapping("/arrayPojo")
public String arrayPojo(String[] hobby){
System.out.println(Arrays.toString(hobby));
return "获取参数~";
}
集合
@RequestMapping("/listParam")
public String listParam(@RequestParam List<String> hobby){
System.out.println(hobby);
return "获取参数~";
}
4.时间参数
@RequestMapping("/timeParam")
public String timeParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime){
System.out.println(updateTime);
return "ok";
}
5.Json参数
@RequestMapping("/jsonParam")
public String jsonParam(@RequestBody User user){
System.out.println(user);
return "ok";
}
6.路径参数
@RequestMapping("/path/{id}/{name}")
public String pathParam(@PathVariable Integer id,@PathVariable String name){
System.out.println(id+":"+name);
return "ok";
}