我们在做企业级开发的时候,免不了要和前端进行数据交互,那么普通类型的数据还好,要是复杂类型的参数,我们怎么去 实现对前端数据的接收了,废话不多说,直接上需求。
我们来看一版数据:如下
{
"name": "李思思",
"appId": "12345",
"MsgObject": {
"MsgContent": "你在哪里啊",
"Kdbr": "331124214207100",
"BhreTime": "2019-07-09 15:45:00",
"MsgType": 2,
"MsgLevel": 3,
"SendTarget": [
{
"RecctNm": "宁采臣",
"DeptNm": "12",
"Mobile": "123568524",
"Adcode": "6352584",
"CityName": "兰若寺",
"SendTime": "2020-04-30 11:08:00"
},
{
"RecctNm": "聂小倩",
"DeptNm": "12",
"Mobile": "1358642",
"Adcode": "6352584",
"CityName": "兰若寺",
"SendTime": "2020-04-30 11:08:00"
}
]
},
"sign": "DWItALe3A",
"baci": "390ef8d411666fedd398f141cacaf0595e17a0cb",
"timestamp": "1588216078862"
}
上面是前端和后台接口交互的一组json数据,大家可以使用josn在线解析工具解析看下,外面是一个大的json对象,里面又包了一个json对象msgObject,msgObject里面又包含了两个SendTarget的json数组,那么我们后台怎么接收到前端发送过来的这些参数了,首先,我们定义一个java模型对象,和这个数据结构保持一致,属性也要保持一致,上代码:
首先导入依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<!-- sprinboot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.61</version>
</dependency>
</dependencies>
public class MsgUnitName implements Serializable {
private String name;
private String appId;
private String sign;
private String baci;
private MsgObject msgObject;
private String timestamp;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
public String getBaci() {
return baci;
}
public void setBaci(String baci) {
this.baci = baci;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public MsgObject getMsgObject() {
return msgObject;
}
public void setMsgObject(MsgObject msgObject) {
this.msgObject = msgObject;
}
@Override
public String toString() {
return "MsgUnitName{" +
"name='" + name + '\'' +
", appId='" + appId + '\'' +
", sign='" + sign + '\'' +
", baci='" + baci + '\'' +
", timestamp='" + timestamp + '\'' +
'}';
}
}
public class MsgObject implements Serializable {
private String msgContent;
private String kdbr;
@JSONField(format="yyyy-MM-dd HH:mm:ss")
private Date bhreTime;
private SendTarget[] sendTarget;
private Integer msgType;
private Integer msgLevel;
public String getMsgContent() {
return msgContent;
}
public void setMsgContent(String msgContent) {
this.msgContent = msgContent;
}
public String getKdbr() {
return kdbr;
}
public void setKdbr(String kdbr) {
this.kdbr = kdbr;
}
public Date getBhreTime() {
return bhreTime;
}
public void setBhreTime(Date bhreTime) {
this.bhreTime = bhreTime;
}
public SendTarget[] getSendTarget() {
return sendTarget;
}
public void setSendTarget(SendTarget[] sendTarget) {
this.sendTarget = sendTarget;
}
public Integer getMsgType() {
return msgType;
}
public void setMsgType(Integer msgType) {
this.msgType = msgType;
}
public Integer getMsgLevel() {
return msgLevel;
}
public void setMsgLevel(Integer msgLevel) {
this.msgLevel = msgLevel;
}
@Override
public String toString() {
return "MsgObject{" +
"msgContent='" + msgContent + '\'' +
", kdbr='" + kdbr + '\'' +
", bhreTime=" + bhreTime +
", sendTarget=" + Arrays.toString(sendTarget) +
", msgType=" + msgType +
", msgLevel=" + msgLevel +
'}';
}
}
public class SendTarget implements Serializable {
private String recctNm;
private String deptNm;
private String mobile;
private String adcode;
private String cityName;
@JSONField(format="yyyy-MM-dd HH:mm:ss")
private Date sendTime;
public String getRecctNm() {
return recctNm;
}
public void setRecctNm(String recctNm) {
this.recctNm = recctNm;
}
public String getDeptNm() {
return deptNm;
}
public void setDeptNm(String deptNm) {
this.deptNm = deptNm;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getAdcode() {
return adcode;
}
public void setAdcode(String adcode) {
this.adcode = adcode;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public Date getSendTime() {
return sendTime;
}
public void setSendTime(Date sendTime) {
this.sendTime = sendTime;
}
@Override
public String toString() {
return "SendTarget{" +
"recctNm='" + recctNm + '\'' +
", deptNm='" + deptNm + '\'' +
", mobile='" + mobile + '\'' +
", adcode='" + adcode + '\'' +
", cityName='" + cityName + '\'' +
", naturalAdnm='" + naturalAdnm + '\'' +
", sendTime=" + sendTime +
'}';
}
}
数据模型定义完了,我们来写Controller层,我们用alibabafastjson JSONObject来接收json参数转换成对应的模型对象。
@RestController
@RequestMapping("/test")
public class DemoController {
@RequestMapping("/add")
public String add(@RequestBody JSONObject jsonObject){
MsgUnitName msgUnitName = JSONObject.toJavaObject(jsonObject,MsgUnitName.class);
System.out.println(msgUnitName.toString());
System.out.println(msgUnitName.getMsgObject().toString());
SendTarget[] sendTarget = msgUnitName.getMsgObject().getSendTarget();
for (int i = 0; i < sendTarget.length; i++) {
System.out.println(Arrays.toString(new SendTarget[]{sendTarget[i]}));
}
System.out.println();
return "success";
}
}
打印结果
MsgUnitName{name='李思思', appId='12345', sign='DWItALe3A', baci='390ef8d411666fedd398f141cacaf0595e17a0cb', timestamp='1588216078862'}
MsgObject{msgContent='你在哪里啊', kdbr='331124214207100', bhreTime=2019-07-09, sendTarget=[SendTarget{recctNm='宁采臣', deptNm='12', mobile='123568524', adcode='6352584', cityName='兰若寺', naturalAdnm='null', sendTime=Thu Apr 30 11:08:00 CST 2020}, SendTarget{recctNm='聂小倩', deptNm='12', mobile='1358642', adcode='6352584', cityName='兰若寺', naturalAdnm='null', sendTime=Thu Apr 30 11:08:00 CST 2020}], msgType=2, msgLevel=3}
[SendTarget{recctNm='宁采臣', deptNm='12', mobile='123568524', adcode='6352584', cityName='兰若寺', naturalAdnm='null', sendTime=Thu Apr 30 11:08:00 CST 2020}]
[SendTarget{recctNm='聂小倩', deptNm='12', mobile='1358642', adcode='6352584', cityName='兰若寺', naturalAdnm='null', sendTime=Thu Apr 30 11:08:00 CST 2020}]
注意时间格式的转换