无法使用 Spring RestTemplate 使用 JSON 数组

编程入门 行业动态 更新时间:2024-10-26 06:37:40
本文介绍了无法使用 Spring RestTemplate 使用 JSON 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

Am trying to populate a DTO from a published RESTful URL (simple HTTP GET) by using Spring's RestTemplate HTTP Client.

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

This is the content of the published JSON that I am trying to consume:

[{"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 = "www.sample/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:

Here's my 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() 方法时,出现以下异常:

When I run the main() method inside Response, I get the following exception:

00:24:14.191 [main] DEBUG org.springframework.web.client.RestTemplate - GET request for "www.sample/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)

我可能做错了什么?

推荐答案

试试这个

public class Response { private static final String URL = "www.sample/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()); } } }

更多推荐

无法使用 Spring RestTemplate 使用 JSON 数组

本文发布于:2023-10-11 13:48:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1481872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   Spring   RestTemplate   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!