Marvo
Marvo

Reputation: 18143

Why isn't JSON being unmarshalled into my Java class?

I'm using Spring Boot and RestTemplate to GET data from an in-house service. The JSON I'm getting back as the response isn't being unmarshalled into my object and I'm unable to figure out why. (I'm consuming other similar REST APIs and those are working, so I've done this before.)

The code for making the request is standard stuff:

public ChecklistResponse getCurrentStatus(String studentId) {
    RestTemplate restTemplate = createRestTemplate();
    HttpHeaders httpHeaders = this.createHeaders();
    String url = this.createItemStatusUrl(studentId);
    ResponseEntity<ChecklistResponse> responseEntity = restTemplate.exchange(url, HttpMethod.GET,
            new HttpEntity<>(httpHeaders), ChecklistResponse.class);
    ChecklistResponse checklistResponse = responseEntity.getBody();
    return checklistResponse;
}

The entire Object tree is large, so I can't really show it here. ChecklistResponse is simple enough, though:

public class ChecklistResponse {

    private HttpStatus httpStatus;
    private String responseType;
    private Response response;

    public ChecklistResponse() {
    }

    ... getters/setters, equals, hashCode, toString ...
}

The start of the JSON response looks like this:

{
    "httpStatus": {
        "code": "200",
        "description": "OK"
    },
    "responseType": "namespaceURI=\"http://bmeta.xxx.com/student/studentChecklistV0.xsd\" element=\"studentChecklist\"",
    "response": {
        "studentChecklist": {
            "identifier": {

I set up interceptors to verify that I'm getting a response back. I ran it with the interceptors disabled, too, so that I know it's not consuming the response (though I'm using the interceptors successfully elsewhere and the object are properly unmarshalling.)

My question is, how can I debug this problem? I've tried enabling Jackson debugging, but that yields nothing. I've set breakpoints throughout the ChecklistResponse class, but it doesn't hit any of those.

Upvotes: 0

Views: 383

Answers (2)

Hasson
Hasson

Reputation: 1914

Make sure that the model object ChecklistResponse correctly represents the map for the JSON object returned by the API you are testing with.

Upvotes: 1

Hasson
Hasson

Reputation: 1914

Most likely you have forgot to add public parameterless constructor, add one like this:

 public ChecklistResponse(){}

you should add these constructors to all classes in the object tree.

Another thing to look at is if the issue is a result of the values in the JSON object, ex, unescaped quotation etc .. try to make a unit test with a mocked object with trivial values but none empty/null ones and see if that test can pass or not, case sensitive issues sometimes cause this to fail as well, set break points in the getters/setters, these should be called in the process, make sure they are public as well as the classes themselves, final thing I can think of is the circular reference, make sure you don't have that as well in your object as this will cause issues.

Upvotes: 3

Related Questions