Spring JSON Serializer和Deserializer没有被调用

时间:2017-01-01 18:04:25

标签: java json spring spring-mongodb spring-json

我为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

}

1 个答案:

答案 0 :(得分:1)

你真的混合了一些东西;)MongoDB确实以类似JSON的格式存储数据(BSON - Binary JSON)但它与Jackson没有任何关系 - 它被用来(就像你展示的那样)序列化/反序列化发送的对象通过HTTP。

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