Search code examples
androidjsonparsingresttemplate

Android Spring Rest Client "exchange" creating the class (406 Not Acceptable)


I've implemented this example Retrieving JSON data via HTTP GET

Json response is something like this:

{"employees":[{"personName":"Albert","personSurname":"1stein","personNumber":"1","personMobile":"5555555","personId":"1234567890","personCompanyCode":"CC01"},
{"personName":"Albert","personSurname":"2stein","personNumber":"2","personMobile":"5555556","personId":"1234567891","personCompanyCode":"CC01"},
{"personName":"Albert","personSurname":"2stein","personNumber":"3","personMobile":"5555557","personId":"1234567892","personCompanyCode":"CC01"},
{"personName":"Albert","personSurname":"4stein","personNumber":"4","personMobile":"5555558","personId":"1234567893","personCompanyCode":"CC01"},
{"personName":"Albert","personSurname":"5stein","personNumber":"5","personMobile":"5555559","personId":"1234567894","personCompanyCode":"CC01"}],
"result":true,"message":"OK"}

I cannot use StringHttpMessageConverter because my data is huge (200,000 employee) It gives me an OutOfMemoryError.

I want to retrieve this data directly from json as in the example but it gives me 406 not acceptable error.

myClass.java :

public class MyClass { private String personName; private String
personSurname; .. ..

(Setters and Getters) .. ..


}

I've tried the following methods, but no difference:

restTemplate.exchange(targetUrl, HttpMethod.GET, requestEntity, myClass.class)

or

restTemplate.exchange(targetUrl, HttpMethod.GET, requestEntity, myClass[].class)

What is the right way to get myClass objects from this json?


Solution

  • You are trying to get the child nodes directly instead of going through the parent node. Rest Template by default tries to convert the response JSON data into the class specified and will throw an error if it couldn't like in your case.

    Here is the employee class

    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.annotation.JsonPropertyOrder;
    
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonPropertyOrder({
    "personName",
    "personSurname",
    "personNumber",
    "personMobile",
    "personId",
    "personCompanyCode"
    })
    public class Employee {
    
    @JsonProperty("personName")
    private String personName;
    @JsonProperty("personSurname")
    private String personSurname;
    @JsonProperty("personNumber")
    private String personNumber;
    @JsonProperty("personMobile")
    private String personMobile;
    @JsonProperty("personId")
    private String personId;
    @JsonProperty("personCompanyCode")
    private String personCompanyCode;
    
    @JsonProperty("personName")
    public String getPersonName() {
    return personName;
    }
    
    @JsonProperty("personName")
    public void setPersonName(String personName) {
    this.personName = personName;
    }
    
    @JsonProperty("personSurname")
    public String getPersonSurname() {
    return personSurname;
    }
    
    @JsonProperty("personSurname")
    public void setPersonSurname(String personSurname) {
    this.personSurname = personSurname;
    }
    
    @JsonProperty("personNumber")
    public String getPersonNumber() {
    return personNumber;
    }
    
    @JsonProperty("personNumber")
    public void setPersonNumber(String personNumber) {
    this.personNumber = personNumber;
    }
    
    @JsonProperty("personMobile")
    public String getPersonMobile() {
    return personMobile;
    }
    
    @JsonProperty("personMobile")
    public void setPersonMobile(String personMobile) {
    this.personMobile = personMobile;
    }
    
    @JsonProperty("personId")
    public String getPersonId() {
    return personId;
    }
    
    @JsonProperty("personId")
    public void setPersonId(String personId) {
    this.personId = personId;
    }
    
    @JsonProperty("personCompanyCode")
    public String getPersonCompanyCode() {
    return personCompanyCode;
    }
    
    @JsonProperty("personCompanyCode")
    public void setPersonCompanyCode(String personCompanyCode) {
    this.personCompanyCode = personCompanyCode;
    }
    
    }
    

    Here is the parent class which encompasses your JSON Response

    import java.util.List;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.annotation.JsonPropertyOrder;
    
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonPropertyOrder({
    "employees",
    "result",
    "message"
    })
    public class EmployeeDataResponse {
    
    @JsonProperty("employees")
    private List<Employee> employees = null;
    @JsonProperty("result")
    private Boolean result;
    @JsonProperty("message")
    private String message;
    
    @JsonProperty("employees")
    public List<Employee> getEmployees() {
    return employees;
    }
    
    @JsonProperty("employees")
    public void setEmployees(List<Employee> employees) {
    this.employees = employees;
    }
    
    @JsonProperty("result")
    public Boolean getResult() {
    return result;
    }
    
    @JsonProperty("result")
    public void setResult(Boolean result) {
    this.result = result;
    }
    
    @JsonProperty("message")
    public String getMessage() {
    return message;
    }
    
    @JsonProperty("message")
    public void setMessage(String message) {
    this.message = message;
    }
    
    }
    

    In this case your rest call should be as follows

    EmployeeDataResponse employeeDataResponse = restTemplate.exchange(targetUrl, HttpMethod.GET, requestEntity, EmployeeDataResponse.class);
    

    Let me know if you need clarification regarding the answer.