当前位置: 代码迷 >> Android >> Android Spring Rest Client“交换”创建类(406不可接受)
  详细解决方案

Android Spring Rest Client“交换”创建类(406不可接受)

热度:91   发布时间:2023-08-04 11:43:36.0

我已经实现了这个示例,

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名员工),它给了我OutOfMemoryError

我想直接从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对象的正确方法是什么?

您正在尝试直接获取子节点,而不是通过父节点。 默认情况下,Rest Template尝试将响应JSON数据转换为指定的类,如果您的情况不满意,将抛出错误。

这是员工班

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

让我知道您是否需要有关答案的说明。

  相关解决方案