使用RestTemplate进行部分JSON检索

使用RestTemplate进行部分JSON检索,json,spring,get,resttemplate,spring-web,Json,Spring,Get,Resttemplate,Spring Web,我正在使用Spring restemplate向第三方服务发送GET请求。它返回巨大的JSON,表示一些实体的列表。但每个实体都非常大,包含大量不必要的数据。我只需要从每个实体获取三个字段。我如何构建我的模型来实现它?例如,如果我们有这个JSON: { "entity1": "foo", "entity2": "bar", "entity3": "...", "entity4": { "aaa": "...", "bbb": "...",

我正在使用
Spring restemplate
向第三方服务发送
GET请求。它返回巨大的
JSON
,表示一些实体的
列表。但每个实体都非常大,包含大量不必要的数据。我只需要从每个实体获取三个字段。我如何构建我的模型来实现它?例如,如果我们有这个
JSON

{
   "entity1": "foo",
   "entity2": "bar",
   "entity3": "...",
   "entity4": {
       "aaa": "...",
       "bbb": "...",
       "ccc": 5
   },
   "entity5": [
       "...",
       "..."
   ]
}, {
   "entity1": "foo",
   "entity2": "bar",
   "entity3": "...",
   "entity4": {
       "aaa": "...",
       "bbb": "...",
       "ccc": 5
   },
   "entity5": [
       "...",
       "..."
   ]
}
我有一门课:

public class SomeModel implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long entity1;
    private String entity2;    
}

如何将此JSON转换为此类的实例数组?

如果使用Jackson,可以使用
@JsonIgnoreProperties(ignoreUnknown=true)
注释模型类,如下所示:

@JsonIgnoreProperties(ignoreUnknown = true)
public class PosterDishModel implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long entity1;
    private String entity2;    
}
基本上,它指示Jackson丢弃接收对象中的任何未知属性

请注意,这不会阻止通过网络传输整个对象,通信量将是相同的,但要反序列化到的对象将不包含不必要的字段和数据