Java Jersey中的Jackson,一个实体具有多个序列化程序

Java Jersey中的Jackson,一个实体具有多个序列化程序,java,json,serialization,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; } 在一些响应中,我希望将该实体序列化为GeoJSO

我正在用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对象作为度量。因此,我编写了两个自定义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;
    }
}
@Provider
公共类ObjectMapperResolver实现ContextResolver{
私有最终对象映射器defaultMapper;
私有最终对象映射器geojsonMapper;
公共对象MapPerResolver(){
defaultMapper=createDefaultMapper();
geojsonMapper=createGeojsonMapper();
}
私有静态对象映射器createDefaultMapper(){
SimpleModule=新的SimpleModule();
module.addSerializer(Measurement.class,新的MeasurementSerializer());
返回新的ObjectMapper()
.enable(SerializationFeature.INDENT_输出)
.注册模块(模块);
}
私有静态对象映射器createGeojsonMapper(){
SimpleModule=新的SimpleModule();
addSerializer(Measurement.class,新的GeoJSONSerializer());
返回新的ObjectMapper()
.enable(SerializationFeature.INDENT_输出)
.注册模块(模块);
}
@凌驾
公共对象映射器getContext(类类型){
if(类型等于(测量等级)){
返回geojsonMapper;
}
否则{
返回默认映射器;
}
}
}

在当前设置中,只要将度量序列化,就会将其序列化为GeoJSON。每当序列化我的其他实体(确实包含度量)时,都会使用defaultMapper。我需要为单个实体类(Measurement.class)切换序列化器/映射器


致以最良好的祝愿

以下是我在spring应用程序中所做的: 1.创建了我的CustomObjectMapper类。 2.创建了特定于类的json生成器方法。 3.在控制器中自动连接所需的对象映射器

Mapper1:

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);
    }
}
Mapper2:

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);
    }
}
在控制器中:

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

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

希望有帮助。

如果您可以为不同的格式使用不同的媒体类型,这很容易实现。不幸的是,我的angular frontend更喜欢只处理JSON。谢谢,我喜欢您的解决方案,可能会使用它。唯一的缺点是每个响应都必须手动序列化。