是否可以使用Jackson来设置Jersey,以使用多个已配置的ObjectMappers
进行序列化/反序列化?
[我想做的是注册一个“默认” Jackson ObjectMapper
,然后能够注册另一个功能,该功能为ObjectMapper
提供一些特殊的配置,在某些情况下会“覆盖”“默认” ObjectMapper
。
例如,此ContextResolver
用于“默认”映射器:
@Provider
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class JacksonMapperProvider implements ContextResolver<ObjectMapper> {
private final ObjectMapper mObjectMapper;
public JacksonMapperProvider() {
mObjectMapper = createMapper();
}
protected abstract ObjectMapper createMapper() {
ObjectMapper mapper = createMapper();
return mapper
.setSerializationInclusion(Include.ALWAYS)
.configure(JsonParser.Feature.ALLOW_COMMENTS, true)
.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return mObjectMapper;
}
}
并且此ContextResolver
将覆盖“默认”映射器:
@Provider
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class SpecializedMapperProvider implements ContextResolver<ObjectMapper> {
private final ObjectMapper mObjectMapper;
public SpecializedMapperProvider() {
mObjectMapper = createMapper();
}
protected abstract ObjectMapper createMapper() {
ObjectMapper mapper = createMapper();
return mapper
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"))
.registerModule(new SpecializedModule1())
.registerModule(new SpecializedModule2());
}
@Override
public ObjectMapper getContext(Class<?> type) {
if(SomeType.isAssignableFrom(type)) {
return mObjectMapper;
}
return null;
}
}
我在JacksonJsonProvider
代码中看到Jackson支持ObjectMapper
提供程序注入/解析。但是,实际上,我看到的是提供程序的“顺序”似乎是随机的(我猜不是,但是我无法弄清楚如何控制顺序)。有时,“替代”位于“默认”之前,并且一切正常,但是在下一次服务器启动时,顺序会更改。
我试图以多种方式使它起作用,包括:
ContextResolver<ObjectMapper>
批注注册ContextResolver<ObjectMapper>
实现我正在使用以下内容:
也许我采用的是完全错误的方法?有没有更好的方法来实现我要完成的任务?也许我应该只定义两个单独的JAX-RS应用程序,并为每个应用程序使用一个ObjectMapper配置?
您可以配置提供程序的顺序,但是在这种情况下,实际上最好使用一个提供程序:
@Provider
最新方法是
@Provider
public class JacksonMapperProvider implements ContextResolver<ObjectMapper> {
private final ObjectMapper defaultMapper;
private final ObjectMapper specializedMapper;
public JacksonMapperProvider() {
defaultMapper = createDefaultMapper();
specializedMapper = createSpecializedMapper();
}
private static ObjectMapper createDefaultMapper() {
return new ObjectMapper()
.setSerializationInclusion(Include.ALWAYS)
.configure(JsonParser.Feature.ALLOW_COMMENTS, true)
.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
}
private static ObjectMapper createSpecializedMapper() {
return new ObjectMapper()
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"))
.registerModule(new SpecializedModule1())
.registerModule(new SpecializedModule2());
}
@Override
public ObjectMapper getContext(Class<?> type) {
if (SomeType.isAssignableFrom(type)) {
return specializedMapper;
}
else {
return defaultMapper;
}
}
}
在最新版本的杰克逊中获得ALLOW_UNQUOTED_FIELD_NAMES。