我已经按照教程http://spring.io/guides/gs/consuming-rest/来使用休息服务。本教程仅提到单层JSON文件。但是,我想解析一个类似
的jsonFoo: {
fooz: 'stringdescripoers}
bar : {
baz: "something",
etc: [[1,2],[2,0]]
},
{another object like bar},
{etc}
}
根据maven与maven建立项目我有以下主要类
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
public class Application {
public static void main(String args[]) {
RestTemplate restTemplate = new RestTemplate();
FeatureCollection collection = restTemplate.getForObject("http://www.trafficlink-online.nl/trafficlinkdata/wegdata/TrajectSensorsNH.GeoJSON", FeatureCollection.class);
System.out.println("it worked");
}
}
我的班级看起来像这样。
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class FeatureCollection {
private String type;
public String getType() {
return type;
}
}
当我通过spring-boot运行运行时,我在命令行中得到以下结果:
tons of info,
[INFO] Attaching agents: []
14:54:02.566 [main] DEBUG o.s.web.client.RestTemplate - Created GET request for "http://www.trafficlink-online.nl/trafficlinkdata/wegdata/TrajectSensorsNH.GeoJSON"
14:54:02.634 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [application/json, application/*+json]
14:54:02.719 [main] DEBUG o.s.web.client.RestTemplate - GET request for "http://www.trafficlink-online.nl/trafficlinkdata/wegdata/TrajectSensorsNH.GeoJSON" 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 FeatureCollection] and content type [text/plain;charset=UTF-8]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:108)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:795)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:779)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:559)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:512)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:268)
at Application.main(Application.java:13)
有没有我没见过的东西?
答案 0 :(得分:0)
Could not extract response: no suitable HttpMessageConverter found for response type [class FeatureCollection] and content type [text/plain;charset=UTF-8]
检查您的依赖项,看看你是否有Jackson Message转换器。您需要将您正在使用的JSON转换为Java对象。如果在你的主类之上使用@SpringBootApplication,那么如果你的依赖关系超出了我的想法,那么Spring会自动将你正在使用的JSON转换为Java对象。
在eclipse中,尝试去新的 - > Spring启动项目 - >并选择网络,也许休息,然后使用该项目来休息。