Jersey客户端无法反序列化json服务-异常(无法反序列化实例) [英] Jersey client can not deserializable json services - exception(can not deserialize instance)

查看:122
本文介绍了Jersey客户端无法反序列化json服务-异常(无法反序列化实例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下用于生成json的API(罐子)球衣. 例如:

Which API (jars) jersey used to generate json by default. example:

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

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

Is it better to use Array? I think it is easier to parse. What you think? example:

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

我有json球衣服务:

I have json jersey services:

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


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

有我的 kategorijaartikla.java

我使用EJB注释进行休眠映射,而使用jaxB进行反序列化和序列化JSON.

I using EJB anotation for hibernate mapping and jaxB for deserializing and serializing JSON.

kategorijaartikla.java

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;
    }
}

有我的泽西岛客户代码:

There is my jersey client code:

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());

呼叫服务后,我有例外:

After call service i have exception:

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

推荐答案

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

Brate, slusaj... I had the same problem. I was building a simple Restful service and JAXB was returning JSON from my JaxB annotated POJO class when GET methods were executed quite well. When my methds were @Consuming JSON (POST, PUT cases) I found out that JAXB had some problems there.

在构建Jersey/JSON服务时,最好使用JACKSON代替JaxB注释(您会发现很多有关谷歌搜索的信息).事实是,JAXB不能很好地完成从JSON到POJO对象的转换(反序列化).当使用XML而不是JSON时,带有@XMLRootElement的JAXB非常适合.

It's better to use JACKSON instead JaxB annotations (you'll find a lot about it googling) when buiilding Jersey / JSON service. The thing is that JAXB doesn't do well the conversion (deserialization) from JSON to your POJO objects. JAXB with its @XMLRootElement is quite suitable when working with XML, but not JSON.

这是我在web.xml中启用杰克逊反序列化/JSON序列化的功能:

This is what I have in web.xml to enable Jackson's deserialization/serialization of 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>

Jersey使用杰克逊需要包含Google for .jar文件. 祝你好运...

Google for .jar file that needs to be included for Jersey to use Jackson... Good luck...

这篇关于Jersey客户端无法反序列化json服务-异常(无法反序列化实例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆