当重写JacksonJsonProvider.writeTo,我可以指定用于序列化的看法? [英] When overriding JacksonJsonProvider.writeTo, can I specify the view used to serialize?

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

问题描述

我有RestEasy的-3.0.6,杰克逊2.2.3和CDI(上Wildfly-8.0.0.CR1运行),一个设置。在我的情况,每个实体都有了扩展,并指定要被序列化的属性的混合类。有两个意见,我们姑且称之为的基本的和的扩展的。因为什么样的视图中的对象,我需要取决于序列化的根的对象,这些意见需要为每对象。所以,我重新使用混合类中了点。例如:

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

现在我正在寻找一种方式来挂钩到杰克逊的序列化过程中指定 JsonJob 作为IFF根实体的观点是工作。我目前使用 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);
}

问题的关键是:这个作品,当且仅当的调用方法进行注释 @JsonView 但是,因为这样做的主要用户是一个通用的超一流的,我不能直接标注添加到方法。可有人提出一个方法要么设置在现有的映射视图( setSerializationConfig()似乎在Jackson-2将消失)或动态添加 @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?

推荐答案

随着杰克逊2.3,可以使用 @JsonView 在JAX-RS终点,如果这将有助于

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天全站免登陆