gpt4 book ai didi

java - Spring JSON 序列化器和反序列化器没有被调用

转载 作者:行者123 更新时间:2023-12-01 09:04:15 34 4
gpt4 key购买 nike

我为 com.google.common.collect.Table 类编写了自定义序列化器和反序列化器。但在 MongoDB 中保留该对象时,它不会被调用。我正在使用 Spring 4、Spring-MongoDB 1.9 和 Jackson 2.8.4。以下是类和配置。你能让我知道这有什么问题吗?我希望在从 MongoDB 进行持久化和检索时调用这些类。

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());
}

类里面的注释

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无关 - 它用于(就像你展示的方式)序列化/反序列化发送的对象通过 HTTP。

在 Spring Data MongoDB 中,您需要实现和配置自定义转换器。有关更多信息,请阅读 here .

关于java - Spring JSON 序列化器和反序列化器没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41417716/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com