尝试使用RestTemplate检索数据时出现空指针异常

时间:2021-12-17 15:43:36

I have the following JSON string:

我有以下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"
                }
            }
        }
]
}
}

When trying to fetch this data, I use the following

在尝试获取此数据时,我使用以下内容

RestTemplate restTemplate = new RestTemplate();
final IssueDTO[] responseEntity = restTemplate.getForObject("http://localhost:8080/im-access/api/issues", Embedded.class).getIssues();

and my Embedded class is this

我的嵌入式课就是这个

@JsonIgnoreProperties(ignoreUnknown = true)
public class Embedded {

    private IssueDTO[] issues;

    public IssueDTO[] getIssues() {
        return issues;
    }

    public void setIssues(IssueDTO[] issues) {
        this.issues = issues;
    }

}

However,I get a null pointer exception when trying to get the data.

但是,在尝试获取数据时,我得到一个空指针异常。

1 个解决方案

#1


1  

It looks like your DTO structure is little off. When I generated your DTOs from http://www.jsonschema2pojo.org/ then I get a structure like below. I think you are missing one level of nesting in your DTO structure, thats why your deserializing is failing. So your code should like this,

看起来你的DTO结构很少。当我从http://www.jsonschema2pojo.org/生成您的DTO时,我得到了如下结构。我认为你在DTO结构中缺少一层嵌套,这就是你的反序列化失败的原因。所以你的代码应该是这样的,

final List<Issues> issues = restTemplate.getForObject(url,WrapperEmbedded.class).getEmbedded().getIssues();

Here is the trimmed version of DTOs, you can try yourself on http://www.jsonschema2pojo.org/ (make sure to select JSON as source type)

这是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

}

#1


1  

It looks like your DTO structure is little off. When I generated your DTOs from http://www.jsonschema2pojo.org/ then I get a structure like below. I think you are missing one level of nesting in your DTO structure, thats why your deserializing is failing. So your code should like this,

看起来你的DTO结构很少。当我从http://www.jsonschema2pojo.org/生成您的DTO时,我得到了如下结构。我认为你在DTO结构中缺少一层嵌套,这就是你的反序列化失败的原因。所以你的代码应该是这样的,

final List<Issues> issues = restTemplate.getForObject(url,WrapperEmbedded.class).getEmbedded().getIssues();

Here is the trimmed version of DTOs, you can try yourself on http://www.jsonschema2pojo.org/ (make sure to select JSON as source type)

这是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

}