在我的 Spring MVC Java 应用程序中,我使用的是 Jersey REST API。我正在使用杰克逊进行 JSON 处理。我将Genson添加到我的项目中,用于过滤某些类的某些字段。我试图只为此使用Genson,剩下的我想使用Jackson。
但是我在使用 Jersey REST api 时遇到异常,因为当 Jar 位于类路径中时,默认情况下启用了 Jersey 的 Genson(从这个链接知道)。
这是我连接到 Web 服务并使用 Jackson 获取数据并转换为 String 的代码
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 解决此问题?
谢谢