首页 > 解决方案 > 如何在循环 springboot 上手动设置标签名称?

问题描述

我在下面有一个 JSON 数组,我希望能够设置有意义的标签名称而不是实体名称。我正在使用这些数据动态生成表,我希望行的名称通过分配自定义标签名称对最终用户更有意义。记录类型生成新的选项卡名称

这是我的 JSON

[
    {
        "country": "USA",
        "projectId": "USAID2020,
        "case": "2014",
        "recordType": "Identification",
        "itemDetails": [
            {
                "ItemValue": "023",
                "Label": "hA",
                "ItemValueLabel": "023",
                "Name": "hA"
            },
            {
                "ItemValue": "0005",
                "Label": "hUM",
                "ItemValueLabel": "0005",
                "Name": "hUM"
            },
            {
                "ItemValue": "5",
                "Label": "hCounty",
                "ItemValueLabel": "5",
                "Name": "hCounty"
            }
        ]
    },
    {
        "country": "USA",
        "projectId": "USAID2020",
        "case": "2014",
        "recordType": "Eligibility",
        "itemDetails": [
            {
                "ItemValue": "023",
                "Label": "hA",
                "ItemValueLabel": "023",
                "Name": "hA"
            },
            {
                "ItemValue": "0005",
                "Label": "hUM",
                "ItemValueLabel": "0005",
                "Name": "hUM"
            },
            {
                "ItemValue": "0005",
                "Label": "hPUM",
                "ItemValueLabel": "0005",
                "Name": "hPUM"
            }
         
        ]
    }
]

这就是我希望标签出现的方式

[
    {
        "country": "USA",
        "projectId": "USAID2020,
        "case": "2014",
        "recordType": "Identification",
        "itemDetails": [
            {
                "ItemValue": "023",
                "Label": "Area Code",
                "ItemValueLabel": "023",
                "Name": "hA"
            },
            {
                "ItemValue": "0005",
                "Label": "Manager Identification",
                "ItemValueLabel": "0005",
                "Name": "hUM"
            },
            {
                "ItemValue": "5",
                "Label": "County Name",
                "ItemValueLabel": "5",
                "Name": "hCounty"
            }
        ]
    },
    {
        "country": "USA",
        "projectId": "USAID2020",
        "case": "2014",
        "recordType": "Eligibility",
        "itemDetails": [
            {
                "ItemValue": "023",
                "Label": "Area Code",
                "ItemValueLabel": "023",
                "Name": "hA"
            },
            {
                "ItemValue": "0005",
                "Label": "Manager Identification",
                "ItemValueLabel": "0005",
                "Name": "hUM"
            },
            {
                "ItemValue": "0005",
                "Label": "House Code",
                "ItemValueLabel": "0005",
                "Name": "hPUM"
            }
         
        ]
    }
]
            

这是我的逻辑

public ArrayList<Summary> getHouseHoldRecordsByCase(String country, String projectId, String case) {
        ArrayList<Summary> documents= new ArrayList<>();
        Summary summary = new Summary();
        ArrayList<ItemDetails> details = new ArrayList<>();
       
        COVER cover = dataStoreService.getHouseHoldRecordsByCase(country, projectId, caseNumber);
        summary.case = caseNumber;
        summary.recordType = "Identification";
        summary.country = country;
        summary.projectId = projectId;
        Field[] fields = hsecover.getClass().getDeclaredFields();
        for (Field _f:fields){
            try {
                String val = PropertyUtils.getProperty(hsecover, _f.getName()).toString();
                details.add(new ItemDetails(val, _f.getName(), val, _f.getName()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        summary.itemDetails = details;
        documents.add(summary);
        summary = new Summary();
        ELIGILITY elig = dataStoreService.getEliByCase(country, projectId, case);
        summary.recordType = "Eligibility";
        summary.case = case;
        summary.country = country;
        summary.projectId = projectId;
        details = new ArrayList<>();
        fields = elig.getClass().getDeclaredFields();
        for (Field _f:fields){
            try {
                String val = PropertyUtils.getProperty(elig, _f.getName()).toString();
                details.add(new ItemDetails(val, _f.getName(), val, _f.getName()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        summary.itemDetails = details;
        documents.add(summary);
        }
        return documents;
    }

    public COVER getHouseHoldRecordsByCase(String country, String projectId, String case) {
        COVER usa = cover_repository.findByCase(case);
        return usa;
    }

    public ELIGILITY getEliByCase(String country, String projectId, String case) {
        ELIGILITY usa = eligility_repository.findByCaseNumber(case);
        return usa;
    }

我的 ItemDetails 模型类

public class ItemDetails {
    public String ItemValue;
    public String Label;
    public String ItemValueLabel;
    public String Name;

    public ItemDetails(String itemValue, String label, String itemValueLabel, String name) {
        ItemValue = itemValue;
        Label = label;
        ItemValueLabel = itemValueLabel;
        Name = name;
    }
}   

我的总结课

public class Summary {
    public String country;
    public String projectId;
    public String case;
    public String recordType;
    public ArrayList<ItemDetails> itemDetails;
}

数据来自数据库。

如何使用有意义的名称而不是实体名称来设置标签名称?

标签: javaspring-boothibernatejpa

解决方案


您可以@JsonProperty("different names")在每个字段上使用,也可以@JsonSetter("different name")在 setter 方法级别使用。


推荐阅读