Jackson2ObjectMapperBuilder 不序列化对象

Jackson2ObjectMapperBuilder doesn't serialize object

我正在使用 Spring Boot,它有自己的 Jackson 默认序列化。 它在我的任务范围内无法正常工作。 所以我想用我自己的自定义序列化器覆盖 Jackson 的默认序列化。 这是我的代码:

@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
    return new Jackson2ObjectMapperBuilder() {
        @Override
        public void configure(ObjectMapper objectMapper) {
            super.configure(objectMapper);
            objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
            SimpleModule simpleModule = new SimpleModule();
            simpleModule.addSerializer(ZonedDateTime.class, new ZonedDateTimeCustomSerializer());
            objectMapper.registerModule(simpleModule);
            objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
        }
    }.serializerByType(ZonedDateTime.class, new JsonSerializer<ZonedDateTime>() {
        @Override
        public void serialize(ZonedDateTime value,
                              JsonGenerator gen,
                              SerializerProvider serializerProvider) throws IOException {
            gen.writeString(value.getNano() + "");
        }
    });
}


private class ZonedDateTimeCustomSerializer extends JsonSerializer<ZonedDateTime> {
    @Override
    public void serialize(ZonedDateTime value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeString(value.getNano() + "");
    }
}

如你所见,我尝试了一些案例,例如

请提示我如何覆盖默认的 Jackson 序列化程序

要序列化日期格式,您可以这样做

@Component
public class JsonDateSerializer extends JsonSerializer<Date>{
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");//yuor desired format goes here
@Override
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
throws IOException, JsonProcessingException {
String formattedDate = dateFormat.format(date);
gen.writeString(formattedDate);
}
}

你也可以喜欢你的豆子。

你还有另一个选择在你的 POJO 中做这样的事情

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm a z")
private Date date;

#Slowpokebreakingnews

如果要添加自定义序列化程序 - customize Jackson2ObjectMapperBuilderCustomizer 的 Jackson2ObjectMapperBuilde 怎么样:

@Bean
public Jackson2ObjectMapperBuilderCustomizer customizer()
{
    return new Jackson2ObjectMapperBuilderCustomizer()
    {
        @Override
        public void customize(Jackson2ObjectMapperBuilder builder)
        {
            builder.serializerByType(CustomPojo.class, 
                                     new CustomSerializator<CustomPojo>());
            //affects to all dates in all pojos (I hope :) )
            builder.indentOutput(true).dateFormat(new SimpleDateFormat
                                                  ("yyyy-MM-dd'T'HH:mm:ssXXX"));

        }
    };
}

对于没有-Spring-boot-configuration,我重写了configureMessageConverters() :

@Configuration
@EnableWebMvc
...
public class WebConfig extends WebMvcConfigurerAdapter
{

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) 
    {
        Jackson2ObjectMapperBuilder builder = new CustomObjectMapperBuilder();
        converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
    }

}

并定义我的 CustomJackson2ObjectMapperBuilder:

public class CustomObjectMapperBuilder extends Jackson2ObjectMapperBuilder
{
    @Override
    public void configure(ObjectMapper objectMapper)
    {
        super.configure(objectMapper);

        SimpleModule module = new SimpleModule();
        module.addSerializer(Selective.class, new SelectiveSerializer());

        objectMapper.registerModule(module)
            .setSerializationInclusion(JsonInclude.Include.NON_NULL)
            .configure(JsonParser.Feature.ALLOW_COMMENTS, true)
            .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
            .configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
            .configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true)
            .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"));
    }
}