ProcessingException:org.codehaus.jackson.map.JsonMappingException:无法从 START_ARRAY 令牌中反序列化 X 的实例
ProcessingException: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of X out of START_ARRAY token
private <T> T[] getObjects(Class<T> tClass, String version) {
String subUrl = "/versions/" + version;
return ClientBuilder.newClient()
.target(url)
.request()
.get(new GenericType<T[]>(tClass) {});
}
这里是 json 对象
[
{
"id":1,
"name":"test",
"country":"USA"
},
{
"id":2,
"name":"test_1",
"country":"cameroon"
}
]
调用getObjects方法:
X[] x = getObjects(X.class, currentVersionName);
我得到这个异常:
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of X out of START_ARRAY token
at [Source: org.apache.http.conn.EofSensorInputStream@3eb91815; line: 1, column: 1]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromArray(BeanDeserializer.java:875)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:597)
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2704)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315)
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419)
at org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext.readFrom(AbstractReaderInterceptorContext.java:66)
at org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:56)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:248)
... 38 more
我使用那些依赖项:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.1.2.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.1.2.Final</version>
</dependency>
尝试更改:
private <T> T[] getObjects(Class<T> tClass, String version)
到
private <T> T[] getObjects(Class<T[]> tClass, String version)
你的来电者将是
X[] x = getObjects(X[].class, currentVersionName);
private <T> T[] getObjects(Class<T> tClass, String version) {
String subUrl = "/versions/" + version;
return ClientBuilder.newClient()
.target(url)
.request()
.get(new GenericType<T[]>(tClass) {});
}
这里是 json 对象
[
{
"id":1,
"name":"test",
"country":"USA"
},
{
"id":2,
"name":"test_1",
"country":"cameroon"
}
]
调用getObjects方法:
X[] x = getObjects(X.class, currentVersionName);
我得到这个异常:
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of X out of START_ARRAY token
at [Source: org.apache.http.conn.EofSensorInputStream@3eb91815; line: 1, column: 1]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromArray(BeanDeserializer.java:875)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:597)
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2704)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315)
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419)
at org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext.readFrom(AbstractReaderInterceptorContext.java:66)
at org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:56)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:248)
... 38 more
我使用那些依赖项:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.1.2.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.1.2.Final</version>
</dependency>
尝试更改:
private <T> T[] getObjects(Class<T> tClass, String version)
到
private <T> T[] getObjects(Class<T[]> tClass, String version)
你的来电者将是
X[] x = getObjects(X[].class, currentVersionName);