java - Jersey 的 jackson ,一个实体有多个序列化程序

标签 java json serialization jersey jackson

我正在使用 Jersey 2.11 和 Jackson 2.4.0 实现 REST 后端以进行序列化。 我的实体看起来像这样:

public class Measurement {

public Measurement() {
    super();
}

private Long id;
private Double lat;
private Double lon;
private Long timestamp;
private User owner;
}

在某些响应中,我希望将此实体序列化为 GeoJSON,而在其他响应中,我希望使用 User.id 而不是 User 对象序列化为 Measurement。因此,我编写了两个自定义 JsonSerializer 并将它们注册到我的 ObjectMapperResolver 类中。

@Provider
public class ObjectMapperResolver implements ContextResolver<ObjectMapper> {

private final ObjectMapper defaultMapper;
private final ObjectMapper geojsonMapper;

public ObjectMapperResolver() {
    defaultMapper = createDefaultMapper();
    geojsonMapper = createGeojsonMapper();
}

private static ObjectMapper createDefaultMapper() {
    SimpleModule module = new SimpleModule();
    module.addSerializer(Measurement.class, new MeasurementSerializer());

    return new ObjectMapper()
    .enable(SerializationFeature.INDENT_OUTPUT)
    .registerModule(module);
}

private static ObjectMapper createGeojsonMapper() {
    SimpleModule module = new SimpleModule();
    module.addSerializer(Measurement.class, new GeoJSONSerializer());

    return new ObjectMapper()
    .enable(SerializationFeature.INDENT_OUTPUT)
    .registerModule(module);
}

@Override
public ObjectMapper getContext(Class<?> type) {
    if (type.equals(Measurement.class)) {
        return geojsonMapper;
    }
    else {
        return defaultMapper;
    }
}

在当前设置中,每当测量值被序列化时,它就会被序列化为 GeoJSON。每当我的其他实体(确实包含测量值)被序列化时,都会使用 defaultMapper。 我需要为一个实体类 (Measurement.class) 切换 Serializer/Mapper 的可能性

最好的问候!

最佳答案

这是我在 spring 应用程序中所做的: 1. 创建了我的 CustomObjectMapper 类。 2. 创建类特定的 json 生成器方法。 3. 在 Controller 中 Autowiring 所需的对象映射器。

映射器 1:

public class CustomObjectMapper1 extends ObjectMapper {
    public CustomObjectMapper1() {
        super();
        super.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        super.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        super.setDateFormat(df);
    }
    public byte[] generateJson(Object value) throws IOException, JsonGenerationException, JsonMappingException {
        Hibernate4Module hm = new Hibernate4Module();
        hm.configure(Hibernate4Module.Feature.USE_TRANSIENT_ANNOTATION, false);
        hm.configure(Hibernate4Module.Feature.FORCE_LAZY_LOADING, false);
        return super.registerModule(hm).writeValueAsBytes(value);
    }
}

映射器 2:

public class CustomObjectMapper2 extends ObjectMapper {
    public CustomObjectMapper2() {
        super();
        super.setDateFormat(df);
    }
    public byte[] generateJson(Object value) throws IOException, JsonGenerationException, JsonMappingException {
        Hibernate4Module hm = new Hibernate4Module();
        return super.registerModule(hm).writeValueAsBytes(value);
    }
}

在 Controller 中:

...
byte[] json = customObjectMapper1.generateJson(myObject);
return json;

...
byte[] json = customObjectMapper2.generateJson(myObject);
return json;

希望对您有所帮助。

关于java - Jersey 的 jackson ,一个实体有多个序列化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27384859/

相关文章:

java - 如何直接从列表的流 -> 过滤器中收集值?

Java 规范实现 - 从那里获取正在实现的接口(interface)的导入

javascript - 将 XML jQuery 对象转换为字符串

java - 带冒号运算符的文本有什么用(例如 : Test:) in java

json - 如何从 PostgreSQL 中的 Json 数组中获取元素

json - JSONPath API 网关主体映射中的访问变量

javascript - 如何正确使用 JsonUtils.safeEval() 中的 JSArray<T> 对象?

serialization - 如何使用 Json.NET 在 C# 中序列化 "union-like"字段

php从文本字段到数组

java - 充气城堡 : need of bcprov-jdk15 and bcprov-jdk16