我有以下 JSON 字符串:
{
"_embedded": {
"issues": [
{
"projectId": 1,
"description": "description",
"reason": "reason",
"consequence": "consequence",
"category": null,
"severity": "HIGH",
"priority": "HIGH",
"source": "INTERN",
"owner": "owner",
"deadline": 1234567890,
"cost": 0,
"status": "OPEN",
"versionNo": 0,
"creationTimestamp": 1419255929860,
"lastUpdateTimestamp": 1419255929860,
"createdBy": "",
"lastUpdatedBy": "",
"_links": {
"self": {
"href": "http://lcalhost:8080/im-access/api/issues/1"
}
}
},
{
"projectId": 1,
"description": "description",
"reason": "reason",
"consequence": "consequence",
"category": null,
"severity": "HIGH",
"priority": "HIGH",
"source": "INTERN",
"owner": "owner",
"deadline": 1234567890,
"cost": 0,
"status": "OPEN",
"versionNo": 0,
"creationTimestamp": 1418911336913,
"lastUpdateTimestamp": 1418911336913,
"createdBy": "",
"lastUpdatedBy": "",
"_links": {
"self": {
"href": "http://lcalhost:8080/im-access/api/issues/2"
}
}
},
{
"projectId": 1,
"description": "description",
"reason": "reason",
"consequence": "consequence",
"category": null,
"severity": "HIGH",
"priority": "HIGH",
"source": "INTERN",
"owner": "owner",
"deadline": 1234567890,
"cost": 0,
"status": "OPEN",
"versionNo": 0,
"creationTimestamp": 1418911337383,
"lastUpdateTimestamp": 1418911337383,
"createdBy": "",
"lastUpdatedBy": "",
"_links": {
"self": {
"href": "http://lcalhost:8080/im-access/api/issues/3"
}
}
}
]
}
}
当尝试获取此数据时,我使用以下内容
RestTemplate restTemplate = new RestTemplate();
final IssueDTO[] responseEntity = restTemplate.getForObject("http://localhost:8080/im-access/api/issues", Embedded.class).getIssues();
我的嵌入式类(class)是这样的
@JsonIgnoreProperties(ignoreUnknown = true)
public class Embedded {
private IssueDTO[] issues;
public IssueDTO[] getIssues() {
return issues;
}
public void setIssues(IssueDTO[] issues) {
this.issues = issues;
}
}
但是,在尝试获取数据时出现空指针异常。
最佳答案
看起来您的 DTO 结构有点不对劲。当我从 http://www.jsonschema2pojo.org/ 生成您的 DTO 时然后我得到如下结构。我认为您的 DTO 结构中缺少一层嵌套,这就是您的反序列化失败的原因。所以你的代码应该像这样,
final List<Issues> issues = restTemplate.getForObject(url,WrapperEmbedded.class).getEmbedded().getIssues();
这是 DTO 的精简版本,您可以自己尝试 http://www.jsonschema2pojo.org/ (确保选择 JSON 作为源类型)
public class WrapperEmbedded {
@JsonProperty("_embedded")
private com.example.Embedded Embedded;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object> ();
//GETTERS AND SETTERS
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"issues"
})
public class Embedded {
@JsonProperty("issues")
private List<Issue> issues = new ArrayList<Issue>();
//GETTERS AND SETTERS
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"_embedded"
})
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"projectId",
"description",
"reason",
"consequence",
"category",
"severity",
"priority",
"source",
"owner",
"deadline",
"cost",
"status",
"versionNo",
"creationTimestamp",
"lastUpdateTimestamp",
"createdBy",
"lastUpdatedBy",
"_links"
})
public class Issue {
//GETTERS AND SETTERS
}
关于java - 尝试使用 RestTemplate 检索数据时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27799838/