0

我已经为 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;
    }

}

弹簧配置

@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

}
4

1 回答 1

1

你真的混合了一些东西;)MongoDB确实以类似JSON的格式(BSON - 二进制JSON)存储数据,但它与杰克逊无关 - 它用于(就像你展示的方式一样)序列化/反序列化通过HTTP发送的对象。

在 Spring Data MongoDB 中,您需要实现和配置自定义转换器。可以在此处阅读有关此内容的更多信息。

于 2017-01-01T19:28:49.623 回答