Spring JSON 没有调用序列化器和反序列化器
Spring JSON Serializer and Deserializer is not getting called
我已经为 com.google.common.collect.Table class 编写了自定义序列化器和反序列化器。但是在 MongoDB 中保留该对象时不会调用它。我正在使用 Spring 4、Spring-MongoDB 1.9 和 Jackson 2.8.4。下面是 class 和配置。你能告诉我这有什么问题吗?我希望在从 MongoDB.
坚持和检索时调用这些 class
public class TableSeserializer extends StdSerializer<Table> {
/**
*
*/
private static final long serialVersionUID = 1L;
public TableSeserializer() {
this(Table.class);
}
public TableSeserializer(Class<Table> t) {
super(t);
}
@SuppressWarnings("unchecked")
@Override
public void serialize(Table value, JsonGenerator gen, SerializerProvider provider)
throws IOException {
gen.writeStartObject();
value.rowMap().forEach((i,map) ->{
try {
gen.writeNumber((int)i);
((Map)map).forEach((k,v)->{
try {
gen.writeStartObject();
Object object = ((Map)map).get(k);
if (object instanceof HTMLInputTag) {
HTMLInputTag inputTag = (HTMLInputTag) object;
gen.writeObjectField(inputTag.getId(),inputTag);
}else{
gen.writeObjectField(object.toString(),object);
}
gen.writeEndObject();
} catch (IOException e) {
e.printStackTrace();
}
});
} catch (IOException e) {
e.printStackTrace();
}
});
gen.writeEndObject();
}
}
public class TableDeserializer extends StdDeserializer<Table<Integer, String, HTMLInputTag>> {
/**
*
*/
private static final long serialVersionUID = 1L;
protected TableDeserializer(Class<?> vc) {
super(vc);
}
public TableDeserializer() {
this(null);
}
@Override
public Table<Integer, String, HTMLInputTag> deserialize(JsonParser jsonparser, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
String data = jsonparser.getText();
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(data);
return null;
}
}
Spring配置
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
SimpleModule module = new SimpleModule();
module.addSerializer(Table.class, new TableSeserializer());
module.addDeserializer(Table.class, new TableDeserializer());
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder().modules(module);
converters.add(jacksonConverter(builder));
}
@Bean
MappingJackson2HttpMessageConverter jacksonConverter(Jackson2ObjectMapperBuilder builder) {
return new MappingJackson2HttpMessageConverter(builder.build());
}
注释在class
public class OrganizationAttributeMetaData extends CommonDomainAttributes implements Cloneable, Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String attributeName;
private int orgid;
private String uniquecode; //3 Character unique code to uniquely identify
@JsonDeserialize(using=TableDeserializer.class)
@JsonSerialize(using=TableSeserializer.class)
Table<Integer, String, HTMLInputTag> htmlAttributes = null; //row,identifier and HTML
}
你真的混合了一些东西 ;) MongoDB 确实以类似 JSON 的格式存储数据(BSON - 二进制 JSON)但它与 Jackson 无关 - 这是用于(如您展示的方式)serialize/deserialize 通过 HTTP 发送的对象。
在 Spring 数据中 MongoDB 您需要实施和配置自定义转换器。有关更多信息,请阅读 here.
我已经为 com.google.common.collect.Table class 编写了自定义序列化器和反序列化器。但是在 MongoDB 中保留该对象时不会调用它。我正在使用 Spring 4、Spring-MongoDB 1.9 和 Jackson 2.8.4。下面是 class 和配置。你能告诉我这有什么问题吗?我希望在从 MongoDB.
坚持和检索时调用这些 classpublic class TableSeserializer extends StdSerializer<Table> {
/**
*
*/
private static final long serialVersionUID = 1L;
public TableSeserializer() {
this(Table.class);
}
public TableSeserializer(Class<Table> t) {
super(t);
}
@SuppressWarnings("unchecked")
@Override
public void serialize(Table value, JsonGenerator gen, SerializerProvider provider)
throws IOException {
gen.writeStartObject();
value.rowMap().forEach((i,map) ->{
try {
gen.writeNumber((int)i);
((Map)map).forEach((k,v)->{
try {
gen.writeStartObject();
Object object = ((Map)map).get(k);
if (object instanceof HTMLInputTag) {
HTMLInputTag inputTag = (HTMLInputTag) object;
gen.writeObjectField(inputTag.getId(),inputTag);
}else{
gen.writeObjectField(object.toString(),object);
}
gen.writeEndObject();
} catch (IOException e) {
e.printStackTrace();
}
});
} catch (IOException e) {
e.printStackTrace();
}
});
gen.writeEndObject();
}
}
public class TableDeserializer extends StdDeserializer<Table<Integer, String, HTMLInputTag>> {
/**
*
*/
private static final long serialVersionUID = 1L;
protected TableDeserializer(Class<?> vc) {
super(vc);
}
public TableDeserializer() {
this(null);
}
@Override
public Table<Integer, String, HTMLInputTag> deserialize(JsonParser jsonparser, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
String data = jsonparser.getText();
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(data);
return null;
}
}
Spring配置
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
SimpleModule module = new SimpleModule();
module.addSerializer(Table.class, new TableSeserializer());
module.addDeserializer(Table.class, new TableDeserializer());
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder().modules(module);
converters.add(jacksonConverter(builder));
}
@Bean
MappingJackson2HttpMessageConverter jacksonConverter(Jackson2ObjectMapperBuilder builder) {
return new MappingJackson2HttpMessageConverter(builder.build());
}
注释在class
public class OrganizationAttributeMetaData extends CommonDomainAttributes implements Cloneable, Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String attributeName;
private int orgid;
private String uniquecode; //3 Character unique code to uniquely identify
@JsonDeserialize(using=TableDeserializer.class)
@JsonSerialize(using=TableSeserializer.class)
Table<Integer, String, HTMLInputTag> htmlAttributes = null; //row,identifier and HTML
}
你真的混合了一些东西 ;) MongoDB 确实以类似 JSON 的格式存储数据(BSON - 二进制 JSON)但它与 Jackson 无关 - 这是用于(如您展示的方式)serialize/deserialize 通过 HTTP 发送的对象。
在 Spring 数据中 MongoDB 您需要实施和配置自定义转换器。有关更多信息,请阅读 here.