Jersey Jackson data entity filtering JsonMappingException on collection

前端 未结 2 933
一个人的身影
一个人的身影 2021-02-09 06:50

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
    

        
相关标签:
2条回答
  • 2021-02-09 07:33

    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
    
    0 讨论(0)
  • 2021-02-09 07:46

    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);
            }
        });
    }
    

    }

    0 讨论(0)
提交回复
热议问题