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
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
and
StdSerializer.findPropertyFilter
are returning null
.
My solution is:
@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);
}
});
}
}