Jersey Jackson数据实体在集合上过滤JsonMappingException [英] Jersey Jackson data entity filtering JsonMappingException on collection
问题描述
尝试实施可选实体过滤时遇到问题。我有一个抽象类,如下所示:
I have an issue when trying to put in place the "selectable entity filtering". I have an Abstract class like following one:
// In your Pom
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-entity-filtering</artifactId>
</dependency>
....
//Somewhere in resourceConfig: Register entity-filtering selectable feature.
register(SelectableEntityFilteringFeature.class);
property(SelectableEntityFilteringFeature.QUERY_PARAM_NAME, "select");
register(JacksonFeature.class);
... ..
在注册之前可选实体过滤一切正常,我测试了很多。
Before registering the "selectable entity filtering" all was working fine, I tested that a lot.
注册可选实体过滤后,我有以下错误:
And after registering "selectable entity filtering" I have the following error:
[2016-02-15 17:25:36] - DEBUG EntityMapper:116 [http-bio-8080-exec-3] Preparing query INSERT INTO
[2016-02-15 17:25:43] - ERROR JsonMappingExceptionMapper:29 [http-bio-8080-exec-3] Malformed Json!
com.fasterxml.jackson.databind.JsonMappingException: Can not resolve PropertyFilter with id 'java.util.HashMap'; no FilterProvider configured
at com.fasterxml.jackson.databind.ser.std.StdSerializer.findPropertyFilter(StdSerial izer.java:285)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:459)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:29)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129)
at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:851)
at com.fasterxml.jackson.jaxrs.base.ProviderBase.writeTo(ProviderBase.java:650)
at org.glassfish.jersey.jackson.internal.FilteringJacksonJaxbJsonProvider.writeTo(FilteringJacksonJaxbJsonProvider.java:135)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:265)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:250)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:106)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:86)
似乎问题来自于
StdSerializer.findPropertyFilter(StdSerializer.java:285)
protected PropertyFilter findPropertyFilter(SerializerProvider provider,
Object filterId, Object valueToFilter)
throws JsonMappingException
{
FilterProvider filters = provider.getFilterProvider();
// Not ok to miss the provider, if a filter is declared to be needed.
if (filters == null) {
throw new JsonMappingException("Can not resolve PropertyFilter with id '"+filterId+"'; no FilterProvider configured");
}
PropertyFilter filter = filters.findPropertyFilter(filterId, valueToFilter);
// But whether unknown ids are ok just depends on filter provider; if we get null that's fine
return filter;
}
我不明白为什么即使在POST请求中也会激活过滤?奇怪的是我没有在请求中放入select查询参数!
你能帮忙吗?
I don’t understand why the filtering is activated even in POST requests ? The strange thing is I didn’t put the "select" query parameter in the request! Could you please help ?
推荐答案
当你使用 SelectableEntityFilteringFeature 时如果您将Collection作为实体放在Response中,那么您将获得 JsonMappingException 。对我来说这是一个错误。解决方法是你应该将你的集合封装到 GenericEntity 中,以便能够被Jersey-Jackson序列化。
It seems that when you are using the SelectableEntityFilteringFeature and if you are putting Collection as an Entity in Response then you will get a JsonMappingException. For me it is a bug. The work around is you should encapsulate your collection into GenericEntity to be able to be serialized by Jersey-Jackson.
return Response.status(Status.OK)
.entity(new GenericEntity<Set<MyEntity>>(entityIDs) {}).build();
// Use GenericEntity to avoid JsonMappingException because of the new flow with Filtering
这篇关于Jersey Jackson数据实体在集合上过滤JsonMappingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!