gpt4 book ai didi

java - 将响应映射到 DTO 时 spring 中的 RestClientException

转载 作者:行者123 更新时间:2023-11-30 10:35:17 25 4
gpt4 key购买 nike

我收到的 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 类:

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 + "]";
}

}

另一个类是:

@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 类中映射响应时,它抛出异常:

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

异常(exception):

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 无法找到构造函数,或者存在一些与之相关的问题,我想知道更多原因,但使用隔离类是可行的。

关于java - 将响应映射到 DTO 时 spring 中的 RestClientException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41262021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com