覆盖 JacksonJsonProvider.writeTo 时,我可以指定用于序列化的视图吗? [英] When overriding JacksonJsonProvider.writeTo, can I specify the view used to serialize?

查看:22
本文介绍了覆盖 JacksonJsonProvider.writeTo 时,我可以指定用于序列化的视图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了 Resteasy-3.0.6、Jackson-2.2.3 和 CDI(在 Wildfly-8.0.0.CR1 上运行).就我而言,每个实体都有一个混合类,用于扩展它并指定要序列化的属性.有两个视图",我们称它们为BasicExtended.由于我需要什么样的对象视图取决于序列化的根"对象,因此这些视图需要针对每个对象.所以我为此重新使用了混合类.示例:

I have a setup with Resteasy-3.0.6, Jackson-2.2.3 and CDI (running on Wildfly-8.0.0.CR1). In my case, every entity has a mix-in class that extends it and specifies the attributes that are to be serialized. There are two "views", let's call them Basic and Extended. Since what kind of view of an object I need depends on the "root" object of the serialization, these views need to be per object. So I re-use the mix-in classes for that. Example:

public class Job { 
    @Id private Long id;
    @OneToMany(mappedBy="job") private Set<Bonus> bonuses;
}

public class Bonus {
    @Id private Long id;
    @ManyToOne(optional=false) private Job job;
    private BigDecimal value;
}

public abstract class JsonJob extends Job { 
    abstract Long getId();

    @JsonView({ JsonJob.class })
    abstract Set<Bonus> getBonuses();
}

public abstract class JsonBonus extends Bonus {
    abstract BigDecimal getValue();
}

现在我正在寻找一种方法来挂钩 jackson 的序列化过程,以将 JsonJob 指定为一个视图,如果根实体是 Job.我目前正在使用 JacksonJsonProvider#writeTo:

Now I'm looking for a way to hook into jackson's serialization process to specify JsonJob as a view iff the root entity is Job. I'm currently using JacksonJsonProvider#writeTo:

@Override
public void writeTo(Object value, Class<?> type, Type genericType, 
                    Annotation[] annotations, MediaType mediaType, 
                    MultivaluedMap<String, Object> httpHeaders, 
                    OutputStream entityStream) throws IOException {
    Class view = getViewForType(type); // if no mix-in class is set, return Object
    ObjectMapper mapper = locateMapper(type, mediaType);

    // require all properties to be explicitly specified for serialization
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    mapper.registerModule(new SerializationMixinConf()); // registers all mix-ins

    super.writeTo(value, type, genericType, annotations, mediaType, 
        httpHeaders, entityStream);
}

重点是:这是有效的,iff调用方法被注释@JsonView.但是由于这个的主要用户是一个泛型的超类,所以我不能直接在方法中添加注解.有人可以建议一种方法来在现有映射器中设置视图(setSerializationConfig() 似乎在 jackson-2 中消失了)或将 @JsonView 动态添加到 @JsonView代码>注释数组?

The point is: this works, iff the calling method is annotated @JsonView. But since the main user of this is a generic super-class, I can't add the annotation to the method directly. Can someone suggest a way to either set the view in the existing mapper (setSerializationConfig() seems to be gone in jackson-2) or dynamically add the @JsonView to the annotations array?

推荐答案

使用 Jackson 2.3,您可以在 JAX-RS 端点上使用 @JsonView,如果这有帮助的话.

With Jackson 2.3, you can use @JsonView on JAX-RS end points, if that would help.

但如果不是,另一种可能是直接使用ObjectWriter(编写器是从ObjectMapper 创建的),构造具有特定视图的编写器.这是完全动态的,可以在每次调用的基础上完成.这需要某种绕过 JAX-RS 的逻辑,通常您需要返回 StreamingOutput(而不是实际的 POJO),但它是可配置性的终极.

But if not, another possibility is to use ObjectWriter directly (writers are created from ObjectMapper), constructing writer with specific view. This is fully dynamic and can be done on per-call basis. This requires sort of by-passing JAX-RS logic and usually you will need to return StreamingOutput (instead of actual POJO), but it is the ultimate in configurability.

这篇关于覆盖 JacksonJsonProvider.writeTo 时,我可以指定用于序列化的视图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆