SpringBoot-复杂DTO接收参数

后端接收复杂JSON数据

举例:(前端传入对象:包含数组、对象、对象列表)
{
    "sysName": "FSSC",
    "dataId": "22f5df09810150ea",
    "dataVersion": "",
    "returnUrl": "http://fsscdev.sce-re.com/",
    "navigateUrl": "http://fsscdev.sce-re.com/",
    "loginName": "xucc",
    "sysArgs": "合同模版匹配参数",
    "enableFB": false,
    "editorMode": 1,
    "paymentMode": 0,
    "CTMReadOnlyFields": [
        "QianYueLeiXing",
        "FaPiaoLeiXing"
    ],
    "CTMName": "关于 xxx 的采购申请合同",
    "CTMSerialNo": "",
    "CTMFormConfig": {
        "NoValidate": 0,
        "HideMoneyStructures": 0,
        "HidePartyContactAddress": 1,
        "HidePartyContactPhone": 0,
        "HidePartyContactName": 0,
        "HidePartyBankAccount": 0,
        "HidePartyBankName": 0,
        "HidePartyAddress": 0,
        "HidePartyPhone": 0,
        "HidePartyFaRen": 0,
        "HidePartyShuiHao": 0,
        "HideParties": 0,
        "HideRef": 0,
        "HideSN": 0,
        "HideName": 0,
        "HideDateFrom": 0,
        "HideDateTo": 0,
        "HideTax": 0,
        "TaxEditable": 1,
        "PartyShuiHaoEditable": 0,
        "HideComment": 1,
        "InvalidChars": null
    },
    "CTMCompanies": [
        {
            "EID": "",
            "MingChen": "单位名称",
            "FaRen": "法人代表",
            "ShuiHao": "税号",
            "KaiHuHang": "开户行",
            "ZhangHao": "账号",
            "DiZhi": "地址",
            "DianHua": "电话",
            "LianXiRen": "联系人",
            "LianXiRenDianHua": "联系人电话"
        }
    ],
    "CTMMoneyStructures": [
        {
            "Type": "产品采购",
            "Money": 20000.00,
            "Tax": 17.5,
            "Remark": ""
        }
    ],
    "CTMPaymentTask": [
        {
            "PayName": "首付款",
            "PayDate1": "2020-12-01",
            "PayDate2": "2020-12-01",
            "PayMoney": 20000.00,
            "PayTax": 17.5,
            "PayCondition": ""
        }
    ]
}

根据前端传入的对象建立DTO

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SceRequestDTO {
    @JsonProperty(value = "sysName")
    private String sysName;
    @JsonProperty(value = "dataId")
    private String dataId;
    @JsonProperty(value = "dataVersion")
    private String dataVersion;
    @JsonProperty(value = "returnUrl")
    private String returnUrl;
    @JsonProperty(value = "navigateUrl")
    private String navigateUrl;
    @JsonProperty(value = "loginName")
    private String loginName;
    @JsonProperty(value = "sysArgs")
    private String sysArgs;
    @JsonProperty(value = "enableFB")
    private String enableFB;
    @JsonProperty(value = "editorMode")
    private String editorMode;
    @JsonProperty(value = "paymentMode")
    private String paymentMode;
    @JsonProperty(value = "CTMName")
    private String CTMName;
    @JsonProperty(value = "CTMSerialNo")
    private String CTMSerialNo;

    @JsonProperty(value = "CTMReadOnlyFields")
    private String[] ctmReadOnlyFields;
    @JsonProperty(value = "CTMFormConfig")
    private CTMFormConfig ctmFormConfig;
    @JsonProperty(value = "CTMCompanies")
    List<CTMCompanies> ctmCompaniesList;
    @JsonProperty(value = "CTMMoneyStructures")
    List<CTMMoneyStructures> ctmMoneyStructures;
    @JsonProperty(value = "CTMPaymentTask")
    List<CTMPaymentTask> ctmPaymentTaskList;
}

1. DTO中包含的对象
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CTMFormConfig {
    @JsonProperty(value = "NoValidate")
    private int NoValidate;
    @JsonProperty(value = "HideMoneyStructures")
    private int HideMoneyStructures;
    @JsonProperty(value = "HidePartyContactAddress")
    private int HidePartyContactAddress;
    @JsonProperty(value = "HidePartyContactPhone")
    private int HidePartyContactPhone;
    @JsonProperty(value = "HidePartyContactName")
    private int HidePartyContactName;
    @JsonProperty(value = "HidePartyBankAccount")
    private int HidePartyBankAccount;
    @JsonProperty(value = "HidePartyBankName")
    private int HidePartyBankName;
    @JsonProperty(value = "HidePartyAddress")
    private int HidePartyAddress;
    @JsonProperty(value = "HidePartyPhone")
    private int HidePartyPhone;
    @JsonProperty(value = "HidePartyFaRen")
    private int HidePartyFaRen;
    @JsonProperty(value = "HidePartyShuiHao")
    private int HidePartyShuiHao;
    @JsonProperty(value = "HideParties")
    private int HideParties;
    @JsonProperty(value = "HideRef")
    private int HideRef;
    @JsonProperty(value = "HideSN")
    private int HideSN;
    @JsonProperty(value = "HideName")
    private int HideName;
    @JsonProperty(value = "HideDateFrom")
    private int HideDateFrom;
    @JsonProperty(value = "HideDateTo")
    private int HideDateTo;
    @JsonProperty(value = "HideTax")
    private int HideTax;
    @JsonProperty(value = "TaxEditable")
    private int TaxEditable;
    @JsonProperty(value = "PartyShuiHaoEditable")
    private int PartyShuiHaoEditable;
    @JsonProperty(value = "HideComment")
    private int HideComment;
    @JsonProperty(value = "InvalidChars")
    private String InvalidChars;
}
2. DTO中包含的对象集合
package com.hand.hssp.sce.cont.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CTMCompanies {
    @JsonProperty(value = "EID")
    private String EID;
    @JsonProperty(value = "MingChen")
    private String MingChen;
    @JsonProperty(value = "FaRen")
    private String FaRen;
    @JsonProperty(value = "ShuiHao")
    private String ShuiHao;
    @JsonProperty(value = "KaiHuHang")
    private String KaiHuHang;
    @JsonProperty(value = "ZhangHao")
    private String ZhangHao;
    @JsonProperty(value = "DiZhi")
    private String DiZhi;
    @JsonProperty(value = "DianHua")
    private String DianHua;
    @JsonProperty(value = "LianXiRen")
    private String LianXiRen;
    @JsonProperty(value = "LianXiRenDianHua")
    private String LianXiRenDianHua;

}

3. DTO中包含的对象集合
package com.hand.hssp.sce.cont.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CTMMoneyStructures {
    @JsonProperty(value = "Type")
    private String Type;
    @JsonProperty(value = "Money")
    private String Money;
    @JsonProperty(value = "Tax")
    private String Tax;
    @JsonProperty(value = "Remark")
    private String Remark;
}

4. DTO中包含的对象集合
package com.hand.hssp.sce.cont.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CTMPaymentTask {
    @JsonProperty(value = "PayName")
    private String PayName;
    @JsonProperty(value = "PayDate1")
    private String PayDate1;
    @JsonProperty(value = "PayDate2")
    private String PayDate2;
    @JsonProperty(value = "PayMoney")
    private String PayMoney;
    @JsonProperty(value = "PayTax")
    private String PayTax;
    @JsonProperty(value = "PayCondition")
    private String PayCondition;
}

核心注解

 import com.fasterxml.jackson.annotation.JsonProperty;
 @JsonProperty(value = "对象名或者字段名")

xml字符串格式对象转换到插入

1. 前端发送的xml格式字符串
{
    "xmlData": "\"3\" recordsum=\"3\" remitdate=\"20200803\" settamtsum=\"342798.5\" settleno=\"A2020080301\" txnamtsum=\"343000\">202008022020080216293400143035-20200802162805021600001600000022020080215999010621550*********9540安徽省农村信用社001430350014303501000416293411796301河南优麦食品有限公司898410159989713C10.1446广德徐瑞芝0201202008022020080210043900143057-202008021003340233000330000022020080232818.5181.5481699******9239中国光大银行001430570014305701102310043936961201河南优麦食品有限公司898410159989713C10.1459六安鼎诚商贸有限责任公司0203202008022020080211162100171175-20200802111241021500001500000022020080214999010621775*********6611徽商银行001711750017117500700511162158732001河南优麦食品有限公司898410159989713C10.1448泾县章梨子0201"
}
2. 后端接口根据key获取value
String xmlData = map.get("xmlData");
3. 获取目标对象并转化为json对象
JSONObject jsonObject = XML.toJSONObject(xmlData).getJSONObject("acctRequests");
4. 将json对象转化成dto对象
AcctRequestsDTO acctRequestsDTO = JSON.parseObject(JSON.toJSONString(jsonObject),AcctRequestsDTO.class);
5. 将dto对象的值复制给entity对象
BeanUtils.copyProperties(acctRequestDTO,accountsDet);

你可能感兴趣的:(SpringBoot学习,java)