Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用Spring RestTemplate使用JSON数组_Java_Spring Boot_Resttemplate - Fatal编程技术网

Java 无法使用Spring RestTemplate使用JSON数组

Java 无法使用Spring RestTemplate使用JSON数组,java,spring-boot,resttemplate,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

我试图通过使用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());
        }
    }
}
我可能做错了什么?

试试这个

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());
        }
    }
}
公共类响应{
私有静态最终字符串URL=”http://www.sample.com/product";
公共静态列表getCampaigns(){
RestTemplate RestTemplate=新RestTemplate();
ResponseEntity resteEntity=restTemplate.getForEntity(URL,DTO[].class);
DTO[]objects=responseEntity.getBody();
MediaType contentType=responseEntity.getHeaders().getContentType();
HttpStatus statusCode=responseEntity.getStatusCode();
返回数组.asList(对象);
}
public void static main(字符串参数[]){
List dtos=getCampaigns();
用于(DTO DTO:dtos){
System.out.println(dto.toString());
}
}
}
试试这个

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());
        }
    }
}
公共类响应{
私有静态最终字符串URL=”http://www.sample.com/product";
公共静态列表getCampaigns(){
RestTemplate RestTemplate=新RestTemplate();
ResponseEntity resteEntity=restTemplate.getForEntity(URL,DTO[].class);
DTO[]objects=responseEntity.getBody();
MediaType contentType=responseEntity.getHeaders().getContentType();
HttpStatus statusCode=responseEntity.getStatusCode();
返回数组.asList(对象);
}
public void static main(字符串参数[]){
List dtos=getCampaigns();
用于(DTO DTO:dtos){
System.out.println(dto.toString());
}
}
}

改用此
jackson

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

com.fasterxml.jackson.core
杰克逊数据绑定
2.8.7

改用此
jackson

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

com.fasterxml.jackson.core
杰克逊数据绑定
2.8.7

Spring默认情况下注册多个消息转换器。不同的转换器取决于不同的库。只有当这些库被设置为类路径时,相应的转换器才会被激活。默认情况下,Spring会注册几个消息转换器。不同的转换器取决于不同的库。只有当这些库被设置为类路径时,相应的转换器才会被激活。