我有一个使用java和jersey编写的rest api。下面是web api的相关代码。
@GET
@Path("/getAllPhase1")
@Produces(MediaType.APPLICATION_JSON)
public List<Phase1Bean> getAllPhase1()
{
Phase1Interface phase1Interface = new Phase1Impl();
List<Phase1Bean> allPhase1 = phase1Interface.getAllPhase1();
return allPhase1;
}
在我的web应用程序的rest客户机中,我编写了下面的代码。
Client client = ClientBuilder.newClient();
client.target("http://localhost:8080/xxx/rest")
.path("phase1/getAllPhase1")
.request(MediaType.APPLICATION_JSON)
.get(Phase1Bean.class);
不幸的是,这会产生以下错误
Exception in thread "main" javax.ws.rs.client.ResponseProcessingException: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of bean.Phase1Bean out of START_ARRAY token
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@2fb3536e; line: 1, column: 1]
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:806)
at org.glassfish.jersey.client.JerseyInvocation.access$700(JerseyInvocation.java:92)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:700)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:696)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:420)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:316)
at JerseyClientGet.main(JerseyClientGet.java:32)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of bean.Phase1Bean out of START_ARRAY token
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@2fb3536e; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:691)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:685)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1215)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:126)
at com.fasterxml.jackson.databind.ObjectReader._bind(ObjectReader.java:1233)
at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:677)
at com.fasterxml.jackson.jaxrs.base.ProviderBase.readFrom(ProviderBase.java:777)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:256)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:235)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1085)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:808)
at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:326)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:803)
... 10 more
Java Result: 1
我怎样才能解决这个问题?
最佳答案
get方法返回的是一个列表,而不是一个phase1bean对象,因此错误消息中提到了“start_array”标记。您可以将客户机.get()
方法更改为接收列表,然后从中提取phase1bean项。
client.target("http://localhost:8080/xxx/rest")
.path("phase1/getAllPhase1")
.request(MediaType.APPLICATION_JSON)
.get(new GenericType<List<Phase1Bean>>(){});
另一种方法是将列表包装在另一个对象(例如,phase1beanresponse)中,该对象包含列表以及可能与这些项相关的其他元数据。