无法使用Spring RestTemplate使用JSON数组

时间:2017-03-10 07:39:54

标签: java spring-boot resttemplate

尝试使用Spring的RestTemplate HTTP客户端从已发布的RESTful URL(简单HTTP GET)填充DTO。

这是我尝试使用的已发布JSON的内容:

[{"startDate":"2017-01-29","cost":"$50000.00","id":1112,"name":"Porsche"},{"startDate":"2017-03-06","cost":"$27000.00","id":38626,"name":"BMW"}]

我的DTO:

class DTO {
    private String startDate;
    private String cost;
    private String name;

    // Getters and Setters
}

我的回应对象:

public class Response {
    private static final STRING = "http://www.sample.com/product";

    public static List<Object> getCampaigns() {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Object[]> responseEntity = (ResponseEntity) restTemplate.getForEntity(URL, Object[].class);
        Object[] objects = responseEntity.getBody();
        MediaType contentType = responseEntity.getHeaders().getContentType();
        HttpStatus statusCode = responseEntity.getStatusCode();
        return Arrays.asList(objects);
    }

    public void static main (String args []) {
        List<Object> dtos = getCampaigns();
        for (Object dto : dtos) {
            System.out.println(dto.toString());
        }
    }
}

这是我的pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.7</version>
    </dependency>

</dependencies>

当我在Response中运行main()方法时,我得到以下异常:

00:24:14.191 [main] DEBUG org.springframework.web.client.RestTemplate - GET request for "http://www.sample.com/product" resulted in 200 (OK)
Exception in thread "main" org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class [Ljava.lang.Object;] and content type [application/json;charset=utf-8]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:917)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:901)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
    at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:312)

我可能做错了什么?

2 个答案:

答案 0 :(得分:5)

试试这个

public class Response {
    private static final String URL = "http://www.sample.com/product";

    public static List<DTO> getCampaigns() {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<DTO[]> responseEntity = restTemplate.getForEntity(URL, DTO[].class);
        DTO[] objects = responseEntity.getBody();
        MediaType contentType = responseEntity.getHeaders().getContentType();
        HttpStatus statusCode = responseEntity.getStatusCode();
        return Arrays.asList(objects);
    }

    public void static main (String args []) {
        List<DTO> dtos = getCampaigns();
        for (DTO dto : dtos) {
            System.out.println(dto.toString());
        }
    }
}

答案 1 :(得分:0)

使用此jackson

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.7</version>
</dependency>