Java REST API:无法从START_ARRAY标记中反序列化Object的实例

时间:2016-06-11 09:07:02

标签: java rest http get jersey

我有一个使用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

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您的GET方法返回一个List,而不是一个Phase1Bean对象 - 因此错误消息中提到了“START_ARRAY”令牌。您可以更改客户端.get()方法以接收List,然后从那里提取Phase1Bean项。

client.target("http://localhost:8080/xxx/rest")
        .path("phase1/getAllPhase1")
        .request(MediaType.APPLICATION_JSON)
        .get(new GenericType<List<Phase1Bean>>(){});

另一种方法是将列表包装在另一个对象(例如Phase1BeanResponse)中,其中包含列表以及可能与项目相关的其他元数据。