Genson与Jersey JsonBindingException:无法反序列化以键入类java.lang.String [英] Genson with Jersey JsonBindingException: Could not deserialize to type class java.lang.String

查看:179
本文介绍了Genson与Jersey JsonBindingException:无法反序列化以键入类java.lang.String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Spring MVC Java应用程序中,我正在使用Jersey REST API.我正在使用Jackson进行JSON处理.我在项目中添加了 Genson ,用于过滤某些类的某些字段.而且我只想使用Genson,而其余的我想使用Jackson.

In my Spring MVC Java application I'm using Jersey REST API. I am using Jackson for JSON processing. I added Genson to my project for filtering some fields for some classes. And I am trying to use Genson only for that and remaining I want to use Jackson.

但是当Jar在类路径中时,默认情况下使用Jersey REST API时,在Jersey启用的Genson时我遇到异常(要从此链接中了解).

But I am getting exception while using Jersey REST api as Jersey enabled Genson by default when the Jar is in the classpath (Got to know from this link).

这是我连接到Web服务并获取数据并使用Jackson转换为String的代码

Here is my code in connect to web service and get data and convert to String using Jackson

public String connect() throws Exception {
        Client client = Client.create();
        try {
            String auth = new String(Base64.encode(userName + COLON + password));
            WebResource webResource = client
                    .resource(webServiceUrl);
            ClientResponse response = webResource
                    .header(AUTHORIZATION, BASIC + auth).type(APPLICATION_JSON)
                    .accept(APPLICATION_JSON).get(ClientResponse.class);
            //code to check response status code.
                stringResponse = response.getEntity(String.class);

        } catch (ClientHandlerException e) {
            throw new Exception("Not a Valid Url.");
        } finally {
            client.destroy();
        }
        return stringResponse;
}

我正在跟踪异常

javax.ws.rs.WebApplicationException: com.owlike.genson.JsonBindingException: Could not deserialize to type class java.lang.String
    at com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom(GensonJsonConverter.java:130)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:553)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:506)
    at com.myapp.WebHttpClient.connect(WebHttpClient.java:74)
    at com.myapp.connection.connectors.WebConnector.getData(WebConnector.java:39)
    at com.myapp.adapter.impl.SourceAdapter.getData(SourceAdapter.java:29)
    at com.myapp.service.impl.WebServiceImpl.adapter(WebServiceImpl.java:174)
    at com.myapp.service.impl.WebServiceImpl.connect(WebServiceImpl.java:107)
    ... 41 more
Caused by: com.owlike.genson.JsonBindingException: Could not deserialize to type class java.lang.String
    at com.owlike.genson.Genson.deserialize(Genson.java:398)
    at com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom(GensonJsonConverter.java:128)
    ... 48 more
Caused by: com.owlike.genson.stream.JsonStreamException: Readen value can not be converted to String
    at com.owlike.genson.stream.JsonReader.valueAsString(JsonReader.java:200)
    at com.owlike.genson.convert.DefaultConverters$StringConverter.deserialize(DefaultConverters.java:364)
    at com.owlike.genson.convert.DefaultConverters$StringConverter.deserialize(DefaultConverters.java:351)
    at com.owlike.genson.convert.NullConverter$NullConverterWrapper.deserialize(NullConverter.java:56)
    at com.owlike.genson.Genson.deserialize(Genson.java:396)
    ... 49 more

第74行是stringResponse = response.getEntity(String.class);.

如何禁用Genson for Jersey或如何使用Genson解决此问题?

How to disable Genson for Jersey or How can I resolve this issue by using Genson?

谢谢

推荐答案

Genson的最新文档托管在github

The up to date documentation of Genson is hosted on github http://owlike.github.io/genson/.

实际发生的情况

对我来说,尚不清楚您想在哪里使用Genson以及如何使用.您是否仅使用Genson进行序列化?您是通过球衣还是直接使用它?

To me it is not clear where you want to use Genson and how. Are you using Genson for serialization only? Are you using it through jersey or directly?

当您执行getEntity(String.class)时,Jersey要求Genson将传入的json解串为字符串.请注意,一般json格式不允许将字符串作为根值.

When you do getEntity(String.class), Jersey asks Genson to deser the incoming json to a string. Note that in general json format does not allow strings as root values.

解决方案

  1. 升级到jersey 2.X(和最新的Genson版本),在新的Jersey版本中,当您要求输入为字符串时,它不会委派给MessageBodyReader(行为正确的IMO).然后,您可以使用此字符串将其设置为所需的任何类型.

  1. Upgrade to jersey 2.X (and latest Genson release), in the new Jersey version when you ask the input as a string it does not delegate to MessageBodyReader (correct behaviour IMO). Then you can use this string to deser to whatever type you want.

使用ClientResponse中的getEntityInputStream方法获取原始InputStream并将其转换为String.

Use getEntityInputStream method from ClientResponse to get the raw InputStream and convert it to a String.

另一种解决方案是重写Gensons对MessageBodyReader的实现,以不处理对String的请求.

Another solution would be to override Gensons implementation of MessageBodyReader, to not handle deser to String.


拥有多个同时处理JSON转换的库对我来说听起来并不对.特别是因为两者(杰克逊和杰森)都提供了一些相似的功能.我打开了一个问题,允许人们在JAX-RS应用程序中禁用Genson.


Having multiple libs that handle JSON conversion at the same time does not sound right to me. Especially as both (Jackson and Genson) provide some similar features. I opened an issue allowing people to disable Genson in JAX-RS apps.

更新 Genson 1.3及更高版本为在Jax-RS应用程序中禁用Genson .

Update Genson 1.3 and up provide a system to disable Genson in a Jax-RS app.

这篇关于Genson与Jersey JsonBindingException:无法反序列化以键入类java.lang.String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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