Spring integration 无法提取响应:未找到响应的合适HttpMessageConverter
嗯,我正试图在春季使用RestTemplate使用RESTWebService。 该服务返回一个JSON,其中包含如下所示的对象列表Spring integration 无法提取响应:未找到响应的合适HttpMessageConverter,spring-integration,spring-rest,Spring Integration,Spring Rest,嗯,我正试图在春季使用RestTemplate使用RESTWebService。 该服务返回一个JSON,其中包含如下所示的对象列表 [ { "name": "123", "ids": { "y": 36.41666667, "x": 39.58333333, "z": 12 }, "ip": "10.219.90.12", "rate": 67.5, "id": 1 }, { "name":
[
{
"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>>() { });
请任何人帮帮我!
提前感谢。它不适用于默认的MappingJackson2HttpMessageConverter,因为您的服务器将不适当的内容类型作为应用程序/八位字节流返回,其中MappingJackson2HttpMessageConverter仅支持:
public MappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
super(objectMapper, MediaType.APPLICATION_JSON, new MediaType("application", "*+json"));
}
您可以通过setSupportedMediaTypesList supportedMediaTypes为您自己的转换器实例提供自定义,但是,可能最好说服服务器端返回正确的JSON内容类型…默认MappingJackson2HttpMessageConverter不起作用,因为您的服务器将不适当的内容类型作为应用程序/八位字节流返回,而MappingJackson2HttpMessageConverter仅支持:
public MappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
super(objectMapper, MediaType.APPLICATION_JSON, new MediaType("application", "*+json"));
}
您可以通过setSupportedMediaTypesList supportedMediaTypes为您自己的转换器实例提供自定义,但可能最好说服服务器端返回正确的JSON内容类型…为什么使用MediaType.APPLICATION\u OCTET\u STREAM?你在处理文件吗?试过使用headers.setAcceptMediaType.APPLICATION_JSON吗?@Manuel:我还没有试过,因为它在异常情况下抱怨为OCTET_流。将尝试并返回,感谢您回来。@Manuel:尝试使用MediaType.APPLICATION_JSON,但结果相同,但出现异常。您是否在WebConfig中配置了消息转换器?否。我想告诉您,下面的工作原理是:ObjectMapper mapper=new ObjectMapper;List aarList=新阵列列表;字符串json=aarRestClient.getForObjecthttp://123.11.25.333:42317/v1/aar,String.class;aarList=mapper.readValuejson,new-TypeReference{}我的理解是,当我传递[new-parameteredTypereference{}]时,整个过程都应该由RestTemplate完成,对吗?为什么要使用MediaType.APPLICATION\u-OCTET\u流?你在处理文件吗?试过使用headers.setAcceptMediaType.APPLICATION_JSON吗?@Manuel:我还没有试过,因为它在异常情况下抱怨为OCTET_流。将尝试并返回,感谢您回来。@Manuel:尝试使用MediaType.APPLICATION_JSON,但结果相同,但出现异常。您是否在WebConfig中配置了消息转换器?否。我想告诉您,下面的工作原理是:ObjectMapper mapper=new ObjectMapper;List aarList=新阵列列表;字符串json=aarRestClient.getForObjecthttp://123.11.25.333:42317/v1/aar,String.class;aarList=mapper.readValuejson,new-TypeReference{}我的理解是,当我传递[new-parameteredTypereference{}]时,整个过程应该由RestTemplate完成,对吗?是的,你是正确的。谢谢你的回答,阿泰姆!是的,你说得对。谢谢你的回答,阿泰姆!
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]
public MappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
super(objectMapper, MediaType.APPLICATION_JSON, new MediaType("application", "*+json"));
}