Android 春"交换"创建类的 Rest 客户端 (406 不可接受)

标签: json Android
发布时间: 2017/3/4 6:16:17
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我实现了这个示例检索的 JSON 数据通过 HTTP GET

Json 响应有点像这个样子︰

{"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"}

我不能使用 StringHttpMessageConverter 我的数据是巨大的 (200,000 雇员) 因为它给了我发生内存不足错误

我想要直接从 json 如示例所示检索此数据,但是它给了我406 不可接受的错误

myClass.java:

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

(Setters and Getters) .. ..


}

我试过以下方法,但是没有差别︰

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

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

从这个 json 获取 myClass 对象的正确方法是什么?

解决方法 1:

你想要子元素直接而不是去通过父节点的节点。其他模板默认情况下试图响应 JSON 数据转换为指定的类并将抛出一个错误,如果它不能像你的情况。

这里是 employee 类

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;
}

}

这里是的父类别包括您的 JSON 响应

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;
}

}

在这种情况下你休息应该打电话,如下所示

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

让我知道是否你需要澄清相关答案。

赞助商