Jersey Jackson data entity filtering JsonMappingException on collection
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
I am using SecurityEntityFilteringFeature
, and I ran upon the same error.
StdSerializer.findPropertyFilter.getFilterProvider
andStdSerializer.findPropertyFilter
are returning null
.
My solution is:
@Providerpublic class JsonMappingExceptionOnCollectionResponseFilter implements ContainerResponseFilter {@Overridepublic 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); } });}
}