将响应映射到 DTO 时 spring 中的 RestClientException
RestClientException in spring when mapping response to DTO
我收到的 http 请求响应格式如下:
{
"total": 1,
"start": 0,
"count": 1,
"data": [
{
"id": 123,
"cg": {
"total": 1,
"data": [
{
"id": 1,
"name": "xyz"
}
]
},
"_score": 1
}
]
}
我想在执行以下代码时将其映射到 DTO:
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<FinalTestDTO> responseEntity = restTemplate.getForEntity(uri, FinalTestDTO.class);
DTO classes:
public class FinalTestDTO implements Serializable{
private static final long serialVersionUID = 250452811965441459L;
private int total;
private int start;
private int count;
@JsonProperty("data")
private List<TestDTO> data;
public FinalTestDTO() {
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public List<TestDTO> getData() {
return data;
}
public void setData(List<TestDTO> data) {
this.data = data;
}
@Override
public String toString() {
return "FinalJobDTO [total=" + total + ", start=" + start + ", count=" + count + ", data=" + data + "]";
}
}
另一个class是:
@JsonIgnoreProperties(ignoreUnknown = true)
public class TestDTO implements Serializable {
private static final long serialVersionUID = -1738546890129236134L;
private long id;
private TestCg cg;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public TestCg getCg() {
return cg;
}
public void setCg(TestCg cg) {
this.cg = cg;
}
@Override
public String toString() {
return "TestDTO{" +
"id=" + id +
", cg=" + cg +
'}';
}
public class TestCg {
private int total;
@JsonProperty( "data" )
private List<Cg> data;
public TestCg() {
super();
}
@JsonCreator
public TestCg(@JsonProperty("total")int total) {
this.total = total;
}
@JsonCreator
public TestCg(@JsonProperty("data") List<Cg> data) {
this.data = data;
}
@JsonCreator
public TestCg(@JsonProperty("total")int total, @JsonProperty("data")List<Cg> data) {
this.total = total;
this.data = data;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public List<Cg> getData() {
return data;
}
public void setData(List<Cg> data) {
this.data = data;
}
@Override
public String toString() {
return "TestCg{" +
"total=" + total +
", data=" + data +
'}';
}
}
public class Cg implements Serializable {
private static final long serialVersionUID = 4187229577080155505L;
@JsonProperty( "id" )
private int id;
@JsonProperty( "name" )
private String name;
public Cg() {
super();
}
@JsonCreator
public Cg(@JsonProperty( "id" )int id) {
this.id = id;
}
@JsonCreator
public Cg(@JsonProperty( "name" )String name) {
this.name = name;
}
@JsonCreator
public Cg(@JsonProperty( "id" )int id, @JsonProperty( "name" )String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Cg{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
}
当我尝试在 FinalTestDTO class 中映射响应时,它抛出异常:
WARN: org.springframework.http.converter.json.MappingJackson2HttpMessageConverter - Failed to evaluate deserialization for type [simple type, class FinalTestDTO]: com.fasterxml.jackson.databind.JsonMappingException: Unrecognized Type: [null] and
异常:
org.springframework.web.client.RestClientException: Could not extract
response: no suitable HttpMessageConverter found for response type
[class com.bullhorn.DTO.FinalTestDTO] and content type
[application/json;charset=UTF-8] at
org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:110)
at
org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:809)
at
org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java
...
尝试将你所有的内在类提取到独立文件-类。出于某种原因,Jackson 无法找到构造函数或存在一些与之相关的问题,我想知道更多原因,但隔离 类 正在工作。
我收到的 http 请求响应格式如下:
{
"total": 1,
"start": 0,
"count": 1,
"data": [
{
"id": 123,
"cg": {
"total": 1,
"data": [
{
"id": 1,
"name": "xyz"
}
]
},
"_score": 1
}
]
}
我想在执行以下代码时将其映射到 DTO:
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<FinalTestDTO> responseEntity = restTemplate.getForEntity(uri, FinalTestDTO.class);
DTO classes:
public class FinalTestDTO implements Serializable{
private static final long serialVersionUID = 250452811965441459L;
private int total;
private int start;
private int count;
@JsonProperty("data")
private List<TestDTO> data;
public FinalTestDTO() {
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public List<TestDTO> getData() {
return data;
}
public void setData(List<TestDTO> data) {
this.data = data;
}
@Override
public String toString() {
return "FinalJobDTO [total=" + total + ", start=" + start + ", count=" + count + ", data=" + data + "]";
}
}
另一个class是:
@JsonIgnoreProperties(ignoreUnknown = true)
public class TestDTO implements Serializable {
private static final long serialVersionUID = -1738546890129236134L;
private long id;
private TestCg cg;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public TestCg getCg() {
return cg;
}
public void setCg(TestCg cg) {
this.cg = cg;
}
@Override
public String toString() {
return "TestDTO{" +
"id=" + id +
", cg=" + cg +
'}';
}
public class TestCg {
private int total;
@JsonProperty( "data" )
private List<Cg> data;
public TestCg() {
super();
}
@JsonCreator
public TestCg(@JsonProperty("total")int total) {
this.total = total;
}
@JsonCreator
public TestCg(@JsonProperty("data") List<Cg> data) {
this.data = data;
}
@JsonCreator
public TestCg(@JsonProperty("total")int total, @JsonProperty("data")List<Cg> data) {
this.total = total;
this.data = data;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public List<Cg> getData() {
return data;
}
public void setData(List<Cg> data) {
this.data = data;
}
@Override
public String toString() {
return "TestCg{" +
"total=" + total +
", data=" + data +
'}';
}
}
public class Cg implements Serializable {
private static final long serialVersionUID = 4187229577080155505L;
@JsonProperty( "id" )
private int id;
@JsonProperty( "name" )
private String name;
public Cg() {
super();
}
@JsonCreator
public Cg(@JsonProperty( "id" )int id) {
this.id = id;
}
@JsonCreator
public Cg(@JsonProperty( "name" )String name) {
this.name = name;
}
@JsonCreator
public Cg(@JsonProperty( "id" )int id, @JsonProperty( "name" )String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Cg{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
}
当我尝试在 FinalTestDTO class 中映射响应时,它抛出异常:
WARN: org.springframework.http.converter.json.MappingJackson2HttpMessageConverter - Failed to evaluate deserialization for type [simple type, class FinalTestDTO]: com.fasterxml.jackson.databind.JsonMappingException: Unrecognized Type: [null] and
异常:
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.bullhorn.DTO.FinalTestDTO] and content type [application/json;charset=UTF-8] at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:110) at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:809) at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java
...
尝试将你所有的内在类提取到独立文件-类。出于某种原因,Jackson 无法找到构造函数或存在一些与之相关的问题,我想知道更多原因,但隔离 类 正在工作。