Jersey客户端无法反序列化json服务 - 异常(无法反序列化实例)

时间:2012-12-08 08:41:03

标签: json jersey deserialization

默认情况下,哪个API(jars)jersey用于生成json。 例如:

 {
        "kategorijaartikla": [
            {
                "id": "1",
                "kategorija": "kategorija1"
            },
            {
                "id": "2",
                "kategorija": "kategorija2"
            }
        ]
    }

使用Array更好吗?我认为它更容易解析。你认为呢? 例如:

 [
        {
            "id": "1",
            "kategorija": "kategorija1"
        },
        {
            "id": "2",
            "kategorija": "kategorija2"
        }
    ]

我有json jersey服务:

 @GET
@Produces("application/json")
    public List<kategorijaartikla>  GetSveKategorije (){


        return KategorijaArtiklaDAO.getInstance().getAll();
    }

有我的 kategorijaartikla.java

我使用EJB anotation进行hibernate映射,使用jaxB进行反序列化和序列化JSON。

kategorijaartikla.java

@Entity
@XmlRootElement
public class kategorijaartikla {
    /** @pdOid f9032734-7d05-4867-8275-bf10813c3748 */
    @Id
    @GeneratedValue
    private Integer id;
    private String kategorija;

    public kategorijaartikla() {
        // TODO Add your own initialization code here.
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer newId) {
        this.id = newId;
    }

    public String getKategorija() {
        return kategorija;
    }

    public void setKategorija(String newKategorija) {
        this.kategorija = newKategorija;
    }
}

我的球衣客户代码:

ClientConfig clientConfig = new DefaultClientConfig();
        clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
                Boolean.TRUE);
        Client client = Client.create(clientConfig);
        WebResource r = client
                .resource("http://tomcat.fit.ba/asdf/rest/GetAllKategorije");

        List<kategorijaartikla> output = r.get(new GenericType<List<kategorijaartikla>>() {});

        System.out.println("Output from Server .... \n");
        System.out.println(output.size());

拨打电话后,我有例外:

Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@cc4364; line: 1, column: 1]
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:564)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:524)
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:696)
    at com.sun.jersey.api.client.WebResource.get(WebResource.java:196)
    at com.fit.test.Json.GetAllKategorijeJson.main(GetAllKategorijeJson.java:34)
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@cc4364; 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.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:246)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:204)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:194)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:30)
    at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2695)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1308)
    at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419)
    at com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.readFrom(JacksonProviderProxy.java:139)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:554)
    ... 4 more

1 个答案:

答案 0 :(得分:1)

Brate,slusaj ......我遇到了同样的问题。 我正在构建一个简单的Restful服务,当GET方法执行得非常好时,JAXB从我的JaxB注释的POJO类返回JSON。当我的方法是@Consuming JSON(POST,PUT案例)时,我发现JAXB在那里遇到了一些问题。

当buiilding Jersey / JSON服务时,最好使用JACKSON而不是JaxB注释(你会发现很多关于它的谷歌搜索)。问题是JAXB在从JSON到POJO对象的转换(反序列化)方面表现不佳。带有@XMLRootElement的JAXB非常适合使用XML,但不适用于JSON。

这是我在web.xml中启用Jackson的反序列化/序列化JSON:

<!--  JAXB works great with XML but with JSON it's much better to use Jackson. Jersey will use Jackson -->
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>

Google for .jar文件需要包含在Jersey中才能使用Jackson ... 祝你好运......