一、集合参数(JSON格式)
1.1-导入"JSON"坐标(通用设置一次就可以)
<!-- springmvc 需要的json相关坐标-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.0</version>
</dependency>
1.2-在"config-->springMvcConfig"加入@EnableWebMvc
注解(通用设置一次就可以)
1.3-外部按照JSON格式的数据传入:["aaa","bbb","ccc"]
1.4-在"行参"里面加入@RequestBody
注解
二、POJO参数(json格式):实体类
2.1-外部按照JSON格式的数据传入:
{
"name":"chenxiix",
"age":"18",
"address":{
"province":"beijing",
"city":"beijing"
}
}
2.2-PostMan测试:外部按照JSON格式的数据传入:
2.3-在"行参"里面加入@RequestBody
注解
User这个实体类的代码块
public class User {
private String name;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
'}';
}
private int age;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
嵌套的实体类Address代码块
public class Address {
private String province ;
private String city;
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
", city='" + 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;
}
}
三、"集合"中有多个对象
3.1-外部按照JSON格式的数据传入:
[
{"name":"chenxixi","age":"18" ,
"addrsss":{"province":"beijng","city":"beijing"}
},
{"name":"wangwang","age":"18" ,
"addrsss":{"province":"beijng","city":"beijing"}
}
]
3.2-PostMan测试:(外部按照JSON格式的数据传入)
3.3-在"行参"里面加入@RequestBody注解:
User这个实体类的代码块
public class User {
private String name;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
'}';
}
private int age;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
嵌套的实体类Address代码块
public class Address {
private String province ;
private String city;
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
", city='" + 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;
}
}
四、总结;
4.1:本节的主要三个"JSON"参数
4.2:步骤1,导入json坐标
4.3:步骤2,设置发生JSON数据
4.4:步骤3,开启自动转换JSON数据支持
4.5:步骤4:,设置接收JSON数据
4.6:结尾
、