I'm implementing a REST backend with Jersey 2.11 and Jackson 2.4.0 for serialization. My entity looks like this:
我正在使用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;
}
In some responses I'd like this entity serialized as GeoJSON and in others as Measurement with the User.id instead of the User object. Therefore I wrote two custom JsonSerializers and registered them in my ObjectMapperResolver class.
在一些响应中,我希望将这个实体序列化为GeoJSON,在另一些响应中作为与用户的度量。id而不是User对象。因此,我编写了两个自定义JsonSerializers,并将它们注册到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;
}
}
}
}
In the current setup whenever a Measurement is serialized its serialized as GeoJSON. Whenever my other entities (which do contain Measurements) are serialized the defaultMapper is used. I'd need a possibility to switch Serializer/Mapper for one single entity class (Measurement.class)
在当前设置中,当度量被序列化为GeoJSON时。当我的其他实体(确实包含度量)被序列化时,使用defaultMapper。我需要为单个实体类(measure .class)切换序列化器/映射器的可能性
Best regards!
最好的问候!
1 个解决方案
#1
1
Here is what I did in my spring application: 1. Created my CustomObjectMapper classes. 2. Created class specific json generator methods. 3. Autowired required object mapper in the controller.
以下是我在spring应用程序中所做的:1。创建我的CustomObjectMapper类。2。创建类特定的json生成器方法。3所示。在控制器中自动连接所需的对象映射器。
Mapper1:
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:
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);
}
}
In controller:
控制器:
...
byte[] json = customObjectMapper1.generateJson(myObject);
return json;
...
byte[] json = customObjectMapper2.generateJson(myObject);
return json;
Hope it helps.
希望它可以帮助。
#1
1
Here is what I did in my spring application: 1. Created my CustomObjectMapper classes. 2. Created class specific json generator methods. 3. Autowired required object mapper in the controller.
以下是我在spring应用程序中所做的:1。创建我的CustomObjectMapper类。2。创建类特定的json生成器方法。3所示。在控制器中自动连接所需的对象映射器。
Mapper1:
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:
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);
}
}
In controller:
控制器:
...
byte[] json = customObjectMapper1.generateJson(myObject);
return json;
...
byte[] json = customObjectMapper2.generateJson(myObject);
return json;
Hope it helps.
希望它可以帮助。