【发布时间】:2015-06-15 11:38:42
【问题描述】:
我找到了一种方法来实现我自己的扩展JsonSerializer<Foo> 的序列化方法,覆盖serialize() 方法并将SimpleModule 注册到所有内容。这是我所拥有的:
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonConfig implements ContextResolver<ObjectMapper> {
@Override
public ObjectMapper getContext(Class<?> aClass) {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
SimpleModule fooModule = new SimpleModule("FooModule",
new Version(0, 1, 0, null, "org.foo.bar", "foo"));
relationshipModule.addSerializer(Foo.class, new FooJacksonSerializer());
relationshipModule.addDeserializer(Foo.class, new FooJacksonDeserializer());
mapper.registerModule(fooModule);
return mapper;
}
}
但是,有没有办法使用两种自定义序列化策略并根据 REST 请求(我使用 RestEasy)选择正确的一种? IE。即时以编程方式更改它,而不是在设置上下文时。我想要Foo 的简洁形式和详细的形式。
【问题讨论】: