设置Springboot返回jackson数据序列化
LocalDateTime格式化
使用下面的配置相当于全局配置就不需要给字段添加@JsonFormat(pattern = DateUt.YYYY_MM_DD_HH_MM_SS)
如果个别字段需要不同的配置可使用@JsonFormat(pattern = DateUt.YYYY_MM_DD),@JsonFormat会优先使用,反序列化亦可使用@JsonFormat注解
package com.example.mpdemo.config; import com.example.mpdemo.common.IBaseEnum; import com.example.mpdemo.util.DateUt; 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.boot.jackson.JsonComponentModule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; /** * @from: fhadmin.cn * @Date: 2021/8/19 * @Description: 时间统一格式化 */ @Configuration public class JSONFormatConfig { private static final DateTimeFormatter ymdhms = DateTimeFormatter.ofPattern(DateUt.YYYY_MM_DD_HH_MM_SS); private static final DateTimeFormatter ymd = DateTimeFormatter.ofPattern(DateUt.YYYY_MM_DD); private static final DateTimeFormatter hms = DateTimeFormatter.ofPattern(DateUt.HH_MM_SS); @Bean @Primary public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); JavaTimeModule javaTimeModule = new JavaTimeModule(); //序列化设置时间格式 javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(ymdhms)); javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(ymd)); javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(hms)); //反序列化设置时间格式 javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(ymdhms)); javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(ymd)); javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(hms)); objectMapper.registerModule(javaTimeModule); //序列化枚举值为数据库存储值 缺少后打印枚举不能转成对应值,需自定义toString方法/或者使用@JsonValue注解 //objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true); //设置序列化枚举格式 JsonComponentModule jsonComponentModule = new JsonComponentModule(); jsonComponentModule.addSerializer(IBaseEnum.class,new BaseEnumSerializer()); objectMapper.registerModule(jsonComponentModule); return objectMapper; } }
Date格式化
#Date类型生效 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8
枚举格式化
把下面的类注册在objectMapper中
//设置序列化枚举格式 JsonComponentModule jsonComponentModule = new JsonComponentModule(); jsonComponentModule.addSerializer(IBaseEnum.class,new BaseEnumSerializer()); objectMapper.registerModule(jsonComponentModule);
根据业务需求调整需要格式化的结构
package com.example.mpdemo.config; import com.example.mpdemo.common.IBaseEnum; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.std.StdSerializer; import org.springframework.boot.jackson.JsonComponent; import java.io.IOException; /** * @from: fhadmin.cn * @Date: 2021/8/19 * @Description: 组织类型枚举序列化 */ //可以使用JsonSerializer public class BaseEnumSerializer extends JsonSerializer<IBaseEnum> { //public class BaseEnumSerializer extends StdSerializer<IBaseEnum> { //protected BaseEnumSerializer(){ // super(IBaseEnum.class); //} @Override public void serialize(IBaseEnum value, JsonGenerator gen, SerializerProvider provider) throws IOException { //返回枚举对象值字符串 //gen.writeString(value.getValue().toString()); //返回枚举对象值 gen.writeObject(value.getValue()); //enum2Object(value, gen); } /** * 把枚举转成对象 */ private void enum2Object(IBaseEnum value, JsonGenerator gen) throws IOException { gen.writeStartObject(); gen.writeFieldName(IBaseEnum.ID); gen.writeObject(value.getValue()); gen.writeFieldName(IBaseEnum.NAME); gen.writeString(value.getName()); gen.writeEndObject(); } }
使用注解方式序列化枚举
使用@JsonValue注解加在需要序列化的字段上,可省略注册配置,只能显示对应的值,不可扩展,如需要扩展使用@JsonSerialize(using = BaseEnumSerializer.class)注解
package com.example.mpdemo.enums; import com.example.mpdemo.common.IBaseEnum; import com.example.mpdemo.config.BaseEnumSerializer; import com.fasterxml.jackson.annotation.JsonValue; import com.fasterxml.jackson.databind.annotation.JsonSerialize; /** * @from: fhadmin.cn * @Date: 2021/8/17 * @Description: 是否删除 */ //不使用全局配置可使用该注解 //@JsonSerialize(using = BaseEnumSerializer.class) public enum DeleteEnum implements IBaseEnum<Boolean> { DELETE_YES(true,"已删除"), DELETE_NO(false,"未删除"); //使用JsonValue注解可格式化显示值 @JsonValue private final boolean id; private final String name; DeleteEnum(boolean id, String name) { this.id = id; this.name = name; } @Override public Boolean getValue() { return this.id; } @Override public String getName() { return this.name; } //使用JsonValue注解可注释掉toString /*@Override public String toString() { //根据情况给前台数据展示deleted或descp return String.valueOf(this.deleted); }*/ }
赞 (0)