好吧,我试图在Spring中使用RestTemplate来使用REST Web服务。 该服务返回一个JSON,其中包含对象列表,如下所示。
[
{
"name": "123",
"ids": {
"y": 36.41666667,
"x": 39.58333333,
"z": 12
},
"ip": "10.219.90.12",
"rate": 67.5,
"id": 1
},
{
"name": "123",
"ids": {
"y": 72.5,
"x": 15.16666667,
"z": 12
},
"ip": "10.219.90.13",
"rate": 67.5,
"aarid": 2
},
{
"name": "123",
"ids": {
"y": 0,
"x": 14.33333333,
"z": 12
},
"ip": "10.219.90.14",
"rate": 67.5,
"id": 3
}]
我为此JSON定义了如下POJO。
public class Data {
@JsonProperty("name")
private String name;
@JsonProperty("ip")
private String ip;
@JsonProperty("rate")
private Double rate;
@JsonProperty("id")
private Long id;
@JsonProperty("ids")
private Ids ids;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Double getRate() {
return rate;
}
public void setRate(Double rate) {
this.rate = rate;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Ids getIds() {
return ids;
}
public void setIds(Ids ids) {
this.ids = ids;
}
}
public class Ids {
private Double x;
private Double y;
private Double z;
public void setX(Double x) {
this.x = x;
}
public void setY(Double y) {
this.y = y;
}
public void setZ(Double z) {
this.z = z;
}
public Double getX() {
return x;
}
public Double getY() {
return y;
}
public Double getZ() {
return z;
}
@Override
public String toString() {
return "AARPosition [x=" + x + ", y=" + y + ", z=" + z + "]";
}
}
我尝试使用以下代码来使用该服务:
ResponseEntity<List<Data>> responseEntity = aarRestClient.exchange("http://123.11.25.333/v1/data",
HttpMethod.GET, null, new ParameterizedTypeReference<List<Data>>() { });
我将null传递给上面的实体,即使我创建实体并尝试,也会出现相同的结果。
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
ResponseEntity<List<Data>> responseEntity = aarRestClient.exchange("http://123.11.25.333/v1/data",
HttpMethod.GET, entity , new ParameterizedTypeReference<List<Data>>() { });
我也试过 -
ResponseEntity<Data[]> responseEntity = aarRestClient.getForEntity("http://123.11.25.333:42317/v1/data", Data[].class);
但是,没有运气。在所有情况下我都低于例外。
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class [Lcom.zebra.ala.rtlsclient.Data;] and content type [application/octet-stream]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:835) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:819) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:599) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:289) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at com.zebra.ala.rtlsclient.AARHealthHandler.monitorAARHealth(AARHealthHandler.java:38) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_92]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_92]
可以请任何人帮助我! 提前谢谢。
答案 0 :(得分:5)
它无法使用默认MappingJackson2HttpMessageConverter
,因为您的服务器会将不合适的Content-Type
作为application/octet-stream
返回,其中MappingJackson2HttpMessageConverter
仅支持:
public MappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
super(objectMapper, MediaType.APPLICATION_JSON, new MediaType("application", "*+json"));
}
您可以通过setSupportedMediaTypes(List<MediaType> supportedMediaTypes)
通过自定义提供您自己的转换器实例,但可能最好说服服务器端返回正确的JSON内容类型......