Spring中映射响应到DTO时的RestClientException

时间:2016-12-21 11:42:09

标签: java json spring jackson resttemplate

我收到了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类中映射响应时,它会抛出异常:

  

警告:org.springframework.http.converter.json.MappingJackson2HttpMessageConverter - 无法评估类型[simple type,class FinalTestDTO]的反序列化:com.fasterxml.jackson.databind.JsonMappingException:无法识别的类型:[null]和

例外:

  

org.springframework.web.client.RestClientException:无法解压缩   响应:没有为响应类型找到合适的HttpMessageConverter   [com.bullhorn.DTO.FinalTestDTO]和内容类型   [application / json; charset = UTF-8] at   org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:110)     在   org.springframework.web.client.RestTemplate $ ResponseEntityResponseExtractor.extractData(RestTemplate.java:809)     在   org.springframework.web.client.RestTemplate $ ResponseEntityResponseExtractor.extractData(RestTemplate.java

     

...

1 个答案:

答案 0 :(得分:1)

尝试将所有内部类提取到独立的文件类。由于某些原因杰克逊无法找到构造函数或者存在与之相关的问题,我想知道更多为什么但是隔离类正在工作。