Ran*_*gar 1 spring-integration spring-rest
好吧,我试图在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
}]
Run Code Online (Sandbox Code Playgroud)
我为此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 + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下代码使用该服务:
ResponseEntity<List<Data>> responseEntity = aarRestClient.exchange("http://123.11.25.333/v1/data",
HttpMethod.GET, null, new ParameterizedTypeReference<List<Data>>() { });
Run Code Online (Sandbox Code Playgroud)
我将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>>() { });
Run Code Online (Sandbox Code Playgroud)
我也尝试过 -
ResponseEntity<Data[]> responseEntity = aarRestClient.getForEntity("http://123.11.25.333:42317/v1/data", Data[].class);
Run Code Online (Sandbox Code Playgroud)
但是,没有运气.在所有情况下我都得到以下异常.
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]
Run Code Online (Sandbox Code Playgroud)
可以请任何人帮助我!提前致谢.
它不使用默认的工作MappingJackson2HttpMessageConverter,因为你的服务器返回不合适Content-Type的application/octet-stream,其中MappingJackson2HttpMessageConverter只支持:
public MappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
super(objectMapper, MediaType.APPLICATION_JSON, new MediaType("application", "*+json"));
}
Run Code Online (Sandbox Code Playgroud)
你可以通过自定义提供你自己的转换器实例setSupportedMediaTypes(List<MediaType> supportedMediaTypes),但可能真的会更好地说服服务器端返回正确的JSON内容类型......
| 归档时间: |
|
| 查看次数: |
11046 次 |
| 最近记录: |