简体   繁体   English

Spring Rest客户端,将响应对象的一部分解析为Set

[英]Spring rest client, parse part of response object to Set

I create a simple rest client in spring with jackson for a training. 我在春季与杰克逊一起创建了一个简单的休息客户进行培训。 I have two models Country and League. 我有国家和联赛的两个模特。 Country has: 国家有:

private Set<League> leagues;

@JsonProperty("country_id")
private long apiCountryId;
@JsonProperty("country_name")
private String name;

and of course getters and setters League has just id and name. 当然,接球手和接球手联盟只有ID和名称。 And now I want to get data from apifootbal, the response looks like: 现在我想从apifootbal获取数据,响应如下:

[
    {
        "country_id":"169",
        "country_name":"England",
        "league_id":"62",
        "league_name":"Premier League"
    },
    {
        "country_id":"169",
        "country_name":"England",
        "league_id":"65",
        "league_name":"League 2"
    },
    {
        "country_id":"169",
        "country_name":"England",
        "league_id":"66",
        "league_name":"Nat. League"
    }
]

So 2 first elements are the same, 2 last are different for every object, league is not separate object so I can't just take it out. 所以对于每个对象来说,前两个元素是相同的,最后两个元素是不同的,联赛不是独立的对象,所以我不能只将其取出。 And right now I don't know how to create one Country object what will looks like: 现在,我不知道如何创建一个Country对象,其外观将如下所示:

id: 169,
name: England,
league: [{
    id:"62",
    name:"Premier League"
}, {
    id:"65",
    name:"League 2"
}, {
    id:"66",
    name:"Nat. League"
}]

I know how to create array of country objects where I have all data, but I don't know how separate it to one Country and 3 league objects (and how to add them to collection in country object). 我知道如何在我拥有所有数据的地方创建国家对象数组,但是我不知道如何将其与一个国家和3个联赛对象分开(以及如何将它们添加到国家对象的集合中)。 One option is to add league_id and league_name to country and after iterate through make league objects from them but it's the only way? 一种选择是将League_id和League_name添加到国家/地区,然后从它们中反复创建联盟对象,但这是唯一的方法?

You can write a custom deserializer and write custom logic to handle such data. 您可以编写自定义解串器并编写自定义逻辑来处理此类数据。

Please refer deserialize xml to pojo using jackson xml mapper (The example is in XML but can be similarly used for JSON too) 使用jackson xml映射器将xml反序列化为pojo (该示例为XML,但也可以类似地用于JSON)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM