在RestTemplate Spring中映射JSONArray

时间:2016-02-09 13:05:28

标签: java json spring resttemplate

我正在尝试使用Spring RestTemplate映射此JSONArray:

[{
    "Command": "/usr/sbin/sshd -D",
    "Created": 1454501297,
    "Id": "e00ca61f134090da461a3f39d47fc0cbeda77fbbc0610439d3c16a932686b612",
    "Image": "ubuntu:latest",
    "Labels": {

    },
    "Names": [
        "/nova-c1896fbd-1309-4da2-8d77-b4fe4c02fa8e"
    ],
    "Ports": [

    ],
    "Status": "Up 2 hours"
}, {
    "Command": "/usr/sbin/sshd -D",
    "Created": 1450106126,
    "Id": "7ffc9dbdd200e2c23adec442abd656ed57306955332697cb7da979f36ebf3b22",
    "Image": "ubuntu:latest",
    "Labels": {

    },
    "Names": [
        "/nova-93b9ae40-8135-48b7-ac17-12094603b28c"
    ],
    "Ports": [

    ],
    "Status": "Up 2 hours"
}]

这是ContainersInfo类:

@JsonIgnoreProperties(ignoreUnknown = true)
public class ContainersInfo {


    private String Id;


    private List<String> Names;

    public String getId() {
        return Id;
    }

    public void setId(String id) {
        Id = id;
    }

    public List<String> getNames() {
        return Names;
    }

    public void setNames(List<String> names) {
        Names = names;
    }
}

但是,当我想获取数据时,我得到null

ContainersInfo[] containers = syncRestTemplate.getForObject("http://192.168.1.2:4243/containers/json?all=1", ContainersInfo[].class);

for (int i = 0; i < containers.length; i++)
            System.out.println("id:" + containers[i].getId());

结果输出如下:

  

ID:空

     

ID:空

任何想法,我应该做什么?

1 个答案:

答案 0 :(得分:1)

你的JSON字段名称是pascal case而不是camel case(通常就是这种情况)。将Jackson命名策略设置为PascalCaseStrategy,即将@JsonNaming(PascalCaseStrategy.class)注释添加到ContainersInfo类中。