简体   繁体   English

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

[英]Java REST API: Can not deserialize instance of Object out of START_ARRAY token

I have a REST API written using Java and Jersey. 我有一个使用Java和Jersey编写的REST API。 Below is the related code of the WEB 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;
    }

In my web application's REST client, I wrote the below code. 在我的Web应用程序的REST客户端中,我编写了以下代码。

Client client = ClientBuilder.newClient();

        client.target("http://localhost:8080/xxx/rest")
            .path("phase1/getAllPhase1")
            .request(MediaType.APPLICATION_JSON)
            .get(Phase1Bean.class);

Unfortunatly this generates the below error 不幸的是,这会产生以下错误

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

How can I solve this issue? 我该如何解决这个问题?

Your GET method is returning a List, rather than a single Phase1Bean object - hence the "START_ARRAY" token mentioned in the error message. 您的GET方法返回一个List,而不是单个Phase1Bean对象-因此,错误消息中提到了“ START_ARRAY”标记。 You can change your client .get() method to receive a List, and then extract the Phase1Bean items from there. 您可以更改客户端的.get()方法以接收列表,然后从中提取Phase1Bean项。

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

An alternative would be to wrap your list in another object (Phase1BeanResponse, for example), containing the list and possibly other metadata related to the items. 一种替代方法是将列表包装在另一个对象(例如,Phase1BeanResponse)中,该对象包含列表以及可能与项目相关的其他元数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用START_ARRAY令牌反序列化对象实例 - Can not deserialize instance of object out of START_ARRAY token 无法从 START_ARRAY 令牌反序列化对象的实例 - Can not deserialize instance of Object out of START_ARRAY token REST Api - JSON - “无法反序列化 ZA8CFDE63311BD59EB26AC96F 的 START_CFDE6331BD54EB26 的实例”6AC96F - REST Api - JSON - “Cannot deserialize instance of object out of START_ARRAY token” 无法反序列化实例,超出 START_ARRAY 令牌 - Can not deserialize instance, out of START_ARRAY token 无法从START_ARRAY标记中反序列化Task的实例 - Can not deserialize instance of Task out of START_ARRAY token 无法从START_ARRAY令牌中反序列化JsonGen实例:newbee - Can not deserialize instance of JsonGen out of START_ARRAY token : newbee 无法从 START_ARRAY 令牌中反序列化 POJO 实例 - Can not deserialize instance of POJO out of START_ARRAY token com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_ARRAY 令牌中反序列化对象实例 - JAVA - com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token - JAVA 杰克逊数组或字符串:无法从START_ARRAY令牌中反序列化java.lang.String实例 - Jackson array or string: Can not deserialize instance of java.lang.String out of START_ARRAY token Java:Jackson String to Json-无法反序列化START_ARRAY令牌之外的实例 - Java: Jackson String to Json - Can not deserialize instance of out of START_ARRAY token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM