jackson便捷开发

jackson序列化单字母前缀属性的问题

2022-03-05  本文已影响0人  virtual灬zzZ
@Data
public class Building{
    @JsonIgnore
    private Long id;

    private LocalDate createTime;

    private Integer historyYears;

    private String name;

    private String shortname;

    private FamousPerson famousPerson;
}

@Data
public class FamousPerson {
    //@JsonProperty("p_name")
    private String pName;

    private String sex;

    private Integer age;
}

使用jackson进行序列化/反序列化,jackson的配置

@Configuration
public class JacksonConfig {

    public static final String PATTERN = "yyyy-MM-dd HH:mm:ss";

    @Primary
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        // 设置时区
        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));

        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);

        objectMapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);

        // 设置时间格式
        objectMapper.setDateFormat(new SimpleDateFormat(PATTERN));

        JavaTimeModule javaTimeModule = new JavaTimeModule();
        // 序列化配置,针对java8 时间
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));

        //反序列化配置,针对java8 时间
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));

        //注册模块
        objectMapper
                .registerModule(javaTimeModule)
                .registerModule(new Jdk8Module())
                .registerModule(new ParameterNamesModule());
        return objectMapper;
    }
}

可见看到设置了驼峰转化,objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);

测试进行序列化的时候,会将pName序列化成pname,而不是p_name

{"create_time":"2022-03-05","history_years":75,"name":"重庆三峡","shortname":"三峡"
,"famous_person":{"sex":"女","age":66,"pname":"宋庆龄"}}

如果字段属性要设置成只有单字母前缀的驼峰,需要另外在bean中该字段设置@JsonProperty("p_name"),才能达到想要的效果。

@Data
public class FamousPerson {
    @JsonProperty("p_name")
    private String pName;

    private String sex;

    private Integer age;
}

{"create_time":"2022-03-05","history_years":75,"name":"重庆三峡","shortname":"三峡"
,"famous_person":{"sex":"女","age":66,"p_name":"宋庆龄"}}
上一篇下一篇

猜你喜欢

热点阅读