Filtering Jersey Jackson JsonMappingException Data in a Collection

I had a problem trying to set up "selective filtering of objects." I have an abstract class like the following:

// 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 "selective filtering of objects", everything works fine, I tested a lot.

And after registering "selective filtering of objects" 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) 

The problem seems to come from

  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 fine return filter; } 

I don’t understand why filtering is activated even in POST requests? The strange thing: I did not set the query parameter "select" in the query! Could you help me?

+5
source share
2 answers

It seems that when you use SelectableEntityFilteringFeature , and if you insert Collection as an object in the response, you will get a JsonMappingException . This is a mistake for me. The caveat is that you must encapsulate your collection in GenericEntity in order to be able to serialize to 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 
+5
source

I am using SecurityEntityFilteringFeature and I encountered the same error.

StdSerializer.findPropertyFilter.getFilterProvider and StdSerializer.findPropertyFilter return null .

My decision:

 @Provider public class JsonMappingExceptionOnCollectionResponseFilter implements ContainerResponseFilter { @Override public void filter(ContainerRequestContext requestCtx, ContainerResponseContext responseCtx) throws IOException { ObjectWriterInjector.set(new ObjectWriterModifier() { @Override public ObjectWriter modify(EndpointConfigBase<?> endpoint, MultivaluedMap<String, Object> responseHeaders, Object valueToWrite, ObjectWriter w, JsonGenerator g) throws IOException { SimpleFilterProvider filterProvider = new SimpleFilterProvider(); SimpleBeanPropertyFilter simpleBeanPropertyFilter = new SimpleBeanPropertyFilter() { @Override protected boolean include(BeanPropertyWriter writer) { return true; } @Override protected boolean include(PropertyWriter writer) { return true; } }; filterProvider.addFilter("your entity class", simpleBeanPropertyFilter); filterProvider.addFilter("your entity class", simpleBeanPropertyFilter); return w.with(filterProvider); } }); } 

}

0
source

Source: https://habr.com/ru/post/1243152/


All Articles