Dropwizard / Jersey资源 - @JsonView和带注释的Guice方法拦截器

时间:2016-09-12 15:36:35

标签: jersey jackson dropwizard

我正在尝试将我的JSON视图应用于资源的响应。但这些观点似乎从未得到应用。即所有字段都在JSON中返回。

这是我的资源。 debug语句记录了正确的JSON,即应用了视图,因此我知道模型对象和注释是正确的。

@GET @Path("events")
@JsonView(Events.PresentationView.class)
@Produces(MediaType.APPLICATION_JSON)
@Metrics
public Events getEvents() throws JsonProcessingException {

    Events events = eventsWebService.getAllEvents();
    LOG.debug("JSON: {}", objectMapper.writerWithView(Events.PresentationView.class).writeValueAsString(events));

    return events;
}

上面使用的对象映射器与dropwizard环境中配置的对象映射器相同。

在dropwizard配置中是否需要执行任何操作才能使用Jackson JSON视图?

提前致谢

更新。 更新了代码段以显示其他注释,该注释用于创建记录度量标准的Guice方法拦截器。这是问题的原因。删除它并按预期工作。

参考此错误:https://github.com/google/guice/issues/101 并调试Jackson和Jersey代码。看来这个方法: com.fasterxml.jackson.jaxrs.cfg.EndpointConfigBase.addAnnotation(Class<? extends Annotation>, Annotation, boolean) 在度量标准注释存在时,永远不会调用它。

这是因为方法:org.glassfish.jersey.message.internal.WriterInterceptorExecutor.TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorContext, MessageBodyWriter)找不到任何注释。

回到引用的bug。 CGLib将创建未复制注释的资源的代理子类。因此,永远不会找到JsonView注释,因此不会应用。

以上不是解决方案,但至少是解释。

0 个答案:

没有答案