I am having a response of http request in format like:
{
"total": 1,
"start": 0,
"count": 1,
"data": [
{
"id": 123,
"cg": {
"total": 1,
"data": [
{
"id": 1,
"name": "xyz"
}
]
},
"_score": 1
}
]
}
I want to map it into DTO when I am executing below code:
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 + "]";
}
}
and another class is:
@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 + '\'' +
'}';
}
}
}
when I try to map the response in FinalTestDTO class, it throws an exception:
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
...
Try to extract all your inner classes to independent files-classes. For some reason Jackson is not able to find the constructors or there is some problem related with it, I would like to know more why but with isolated classes is working.