jersey / jackson - 基于查询参数过滤属性

时间:2014-10-20 15:09:37

标签: java rest filter jersey jackson

使用Jackson过滤属性非常简单:

final FilterProvider filters = new SimpleFilterProvider().
    addFilter(... the name of the filter ...,
    SimpleBeanPropertyFilter.filterOutAllExcept(... enumeration of properties ...));

<object mapper>.writer(filters).writeValueAsString(... the bean ...);

我正在尝试将其集成到我的Jersey REST应用程序中。 API用户可以通过提供查询字符串来过滤属性:

https://the-api/persons?fields=name,age,location,gender

泽西岛最优雅的方式是什么?我可以轻松地在我的资源方法中执行上述操作,但这会以某种方式杀死Jersey的优雅。此外,我相信为每个请求创建一个新的ObjectMapper都会有性能损失。

我可以写一个MessageBodyWriterfields上下文中获取UriInfo查询参数,并在根据fields查询参数应用过滤器时将实体序列化为json 。这是最好的方法吗?像这样:

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JustTesting implements MessageBodyWriter<Object> {
    @Context
    UriInfo uriInfo;
    @Context
    JacksonJsonProvider jsonProvider;

    public boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
        return MediaType.APPLICATION_JSON_TYPE.equals(mediaType);
    }

    public long getSize(Object object, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
        return -1;
    }

    public void writeTo(Object object, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> stringObjectMultivaluedMap, OutputStream outputStream) throws IOException, WebApplicationException {
        final FilterProvider filters = new SimpleFilterProvider().addFilter("MyFilter", SimpleBeanPropertyFilter.filterOutAllExcept(uriInfo.getQueryParameters().getFirst("fields")));

        jsonProvider.locateMapper(aClass, mediaType).writer(filters).writeValue(outputStream, object);
    }
}

它似乎有效,但我不确定这样做是否聪明。我是泽西岛图书馆的新手。

1 个答案:

答案 0 :(得分:6)

我目前解决类似问题的方法是注册以下servlet过滤器:

@Singleton
public class ViewFilter implements Filter {
    private @Inject Provider<ViewBeanPropertyFilter> filter;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException { }

    @Override
    public void destroy() { }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        ObjectWriterInjector.set(new ObjectWriterModifier() {
            @Override
            public ObjectWriter modify(EndpointConfigBase<?> endpoint, MultivaluedMap<String, Object> responseHeaders, Object valueToWrite, ObjectWriter w, JsonGenerator g) throws IOException {
                return w.with(new FilterProvider() {
                    @Override
                    public BeanPropertyFilter findFilter(Object filterId) {
                        if(filterId.equals(ViewFilterJacksonModule.FILTER_NAME)) {
                            return filter.get();
                        }
                        return null;
                    }
                });
            }
        });
        chain.doFilter(request, response);
    }

    public static class ViewBeanPropertyFilter extends SimpleBeanPropertyFilter {
        private @Inject ViewManager manager;

        @Override
        protected boolean include(BeanPropertyWriter writer) {
            Class<?> cls = writer.getMember().getDeclaringClass();
            return manager.isFieldInView(cls, writer.getMember().getName());
        }

        @Override
        protected boolean include(PropertyWriter writer) {
            return true;
        }
    }
}

它比您提供的解决方案更加粗糙,但保留了标准的JacksonJsonProvider序列化。

可以通过FilterProvider提取m.getConfig().getFilterProvider()并在过滤器之前/之后委托给它来改进它。