jackson序列化配置
import com.JsonGenerator;
import com.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.dition.ConditionalOnMissingBean;
import t.annotation.Bean;
import t.annotation.Configuration;
import t.annotation.Primary;
import org.verter.json.Jackson2ObjectMapperBuilder;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/
**
* @author wqf
* @date 2021/07/11
* 类描述:
*/
@Configuration
public class JacksonConfig {
/**
* 默认⽇期时间格式
*/
private static final String LOCAL_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/
**
* 默认⽇期格式
*/
private static final String LOCAL_DATE_FORMAT = "yyyy-MM-dd";
/**
* 默认时间格式
*/
private static final String DATE_TIME_FORMAT = "HH:mm:ss";
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
//时间序列化
builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
//针对于JDK新时间类。序列化时带有T的问题,⾃定义格式化字符串
JavaTimeModule javaTimeModule = new JavaTimeModule();
//LocalDateTime
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_FORMAT)));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_FORMAT)));        //LocalDate
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(LOCAL_DATE_FORMAT)));
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(LOCAL_DATE_FORMAT)));bigdecimal格式化两位小数
//LocalTime
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
//序列化将BigDecimal转String类型 BigDecimal数据返回到前端会出现精度丢失的问题不建议使⽤
builder.featuresToDisable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
//Include.NON_EMPTY 属性为空("")或者为 NULL 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量
//builder.serializationInclusion(JsonInclude.Include.NON_NULL);
//未知字段不序列化
builder.failOnUnknownProperties(false);
Z⽽不

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。