简体   繁体   English

使用RestTemplate和Jackson反序列化对Java的JSON响应

[英]Deserializing JSON response to Java using RestTemplate and Jackson

I'm running into some headaches with deserializing JSON into Java objects in a Spring Boot 1.5 app with Jackson. 我在使用Jackson的Spring Boot 1.5应用程序中将JSON反序列化为Java对象时遇到了一些麻烦。

As you can see below, the JSON response is an array consisting of a single JSON object with some nested attributes: 如下所示,JSON响应是一个数组,由一个带有一些嵌套属性的JSON对象组成:

[
  {
    "deploymentProject": {
        "id": 57966596,
        "name": "MyApp 6.3"
    },
    "environmentStatuses": [{
            "environment": {
                "id": 57245736,
                "name": "Dev1",
                "deploymentProjectId": 57966596
            },
            "deploymentResult": {
                "deploymentVersionName": "App-51",
                "id": 59769040
            }
        },
        {
            "environment": {
                "id": 57245737,
                "name": "Dev2",
                "deploymentProjectId": 57966596
            },
            "deploymentResult": {
                "deploymentVersionName": "App-51",
                "id": 59769041
            }
        }
    ]
  }
]

ResultData.java ResultData.java

I don't care about the deploymentProject attribute so I'm only including the environmentStatuses in ResultData . 我不关心deploymentProject属性,所以我只包括environmentStatusesResultData

@JsonIgnoreProperties(ignoreUnknown = true)
public class ResultData {   
  private EnvironmentStatus[] environmentStatuses;

  // Getters and setters omitted
}

EnvironmentStatus.java EnvironmentStatus.java

@JsonIgnoreProperties(ignoreUnknown = true)
public class EnvironmentStatus {

    private Environment environment;
    private DeploymentResult deployment;

    // Getters and setters omitted
}

Environment.java Environment.java

@JsonIgnoreProperties(ignoreUnknown = true)
public class Environment {
    private long id;
    private String name;
    private String deploymentProjectId;
    //Getters and setters omitted
}

DeploymentResult.java DeploymentResult.java

@JsonIgnoreProperties(ignoreUnknown = true)
public class DeploymentResult {
    private long id;
    private String deploymentVersionName;
    // Getters and setters omitted
}

When I make the call to RestTemplate in my service class, the environmentStatuses array is null : 当我在服务类中调用RestTemplate时, environmentStatuses数组为null

Service.java Service.java

ResponseEntity<List<ResultData>> response = restTemplate.exchange(uriBuilder.toUriString(),
                HttpMethod.GET, null, new ParameterizedTypeReference<List<ResultData>>() {});

return response.getBody();

private DeploymentResult deployment; is the next issue.. If you want this name in your code you need to annotate with the json structure name. 是下一个问题。.如果要在代码中使用此名称,则需要使用json结构名称进行注释。 or if not name it deploymentResult 或者如果不命名为deploymentResult

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

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