Chinaunix首页 | 论坛 | 博客
  • 博客访问: 614150
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-08-25 12:00:30

 LocalDateTime格式化
使用下面的配置相当于全局配置就不需要给字段添加@JsonFormat(pattern = DateUt.YYYY_MM_DD_HH_MM_SS)
如果个别字段需要不同的配置可使用@JsonFormat(pattern = DateUt.YYYY_MM_DD),@JsonFormat会优先使用,反序列化亦可使用@JsonFormat注解\

点击(此处)折叠或打开

  1. package com.example.mpdemo.config;

  2. import com.example.mpdemo.common.IBaseEnum;
  3. import com.example.mpdemo.util.DateUt;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.fasterxml.jackson.databind.SerializationFeature;
  6. import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
  7. import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
  8. import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
  9. import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
  10. import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
  11. import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
  12. import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
  13. import org.springframework.boot.jackson.JsonComponentModule;
  14. import org.springframework.context.annotation.Bean;
  15. import org.springframework.context.annotation.Configuration;
  16. import org.springframework.context.annotation.Primary;

  17. import java.time.LocalDate;
  18. import java.time.LocalDateTime;
  19. import java.time.LocalTime;
  20. import java.time.format.DateTimeFormatter;

  21. /**
  22.  * @from: fhadmin.cn
  23.  * @Date: 2021/8/19
  24.  * @Description: 时间统一格式化
  25.  */
  26. @Configuration
  27. public class JSONFormatConfig {

  28.     private static final DateTimeFormatter ymdhms = DateTimeFormatter.ofPattern(DateUt.YYYY_MM_DD_HH_MM_SS);

  29.     private static final DateTimeFormatter ymd = DateTimeFormatter.ofPattern(DateUt.YYYY_MM_DD);

  30.     private static final DateTimeFormatter hms = DateTimeFormatter.ofPattern(DateUt.HH_MM_SS);

  31.     @Bean
  32.     @Primary
  33.     public ObjectMapper objectMapper() {
  34.         ObjectMapper objectMapper = new ObjectMapper();
  35.         JavaTimeModule javaTimeModule = new JavaTimeModule();
  36.         //序列化设置时间格式
  37.         javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(ymdhms));
  38.         javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(ymd));
  39.         javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(hms));
  40.         //反序列化设置时间格式
  41.         javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(ymdhms));
  42.         javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(ymd));
  43.         javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(hms));

  44.         objectMapper.registerModule(javaTimeModule);
  45.         //序列化枚举值为数据库存储值 缺少后打印枚举不能转成对应值,需自定义toString方法/或者使用@JsonValue注解
  46.         //objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
  47.         //设置序列化枚举格式
  48.         JsonComponentModule jsonComponentModule = new JsonComponentModule();
  49.         jsonComponentModule.addSerializer(IBaseEnum.class,new BaseEnumSerializer());
  50.         objectMapper.registerModule(jsonComponentModule);
  51.         return objectMapper;
  52.     }
  53. }


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); 

根据业务需求调整需要格式化的结构

点击(此处)折叠或打开

  1. package com.example.mpdemo.config;

  2. import com.example.mpdemo.common.IBaseEnum;
  3. import com.fasterxml.jackson.core.JsonGenerator;
  4. import com.fasterxml.jackson.databind.JsonSerializer;
  5. import com.fasterxml.jackson.databind.SerializerProvider;
  6. import com.fasterxml.jackson.databind.ser.std.StdSerializer;
  7. import org.springframework.boot.jackson.JsonComponent;

  8. import java.io.IOException;

  9. /**
  10.  * @from: fhadmin.cn
  11.  * @Date: 2021/8/19
  12.  * @Description: 组织类型枚举序列化
  13.  */

  14. //可以使用JsonSerializer
  15. public class BaseEnumSerializer extends JsonSerializer<IBaseEnum> {
  16. //public class BaseEnumSerializer extends StdSerializer<IBaseEnum> {

  17.     //protected BaseEnumSerializer(){
  18.     // super(IBaseEnum.class);
  19.     //}

  20.     @Override
  21.     public void serialize(IBaseEnum value, JsonGenerator gen, SerializerProvider provider) throws IOException {
  22.         //返回枚举对象值字符串
  23.         //gen.writeString(value.getValue().toString());
  24.         //返回枚举对象值
  25.         gen.writeObject(value.getValue());
  26.         //enum2Object(value, gen);
  27.     }

  28.     /**
  29.      * 把枚举转成对象
  30.      */
  31.     private void enum2Object(IBaseEnum value, JsonGenerator gen) throws IOException {
  32.         gen.writeStartObject();
  33.         gen.writeFieldName(IBaseEnum.ID);
  34.         gen.writeObject(value.getValue());
  35.         gen.writeFieldName(IBaseEnum.NAME);
  36.         gen.writeString(value.getName());
  37.         gen.writeEndObject();
  38.     }
  39. }


使用注解方式序列化枚举


使用@JsonValue注解加在需要序列化的字段上,可省略注册配置,只能显示对应的值,不可扩展,如需要扩展使用@JsonSerialize(using = BaseEnumSerializer.class)注解

点击(此处)折叠或打开

  1. package com.example.mpdemo.enums;

  2. import com.example.mpdemo.common.IBaseEnum;
  3. import com.example.mpdemo.config.BaseEnumSerializer;
  4. import com.fasterxml.jackson.annotation.JsonValue;
  5. import com.fasterxml.jackson.databind.annotation.JsonSerialize;

  6. /**
  7.  * @from: fhadmin.cn
  8.  * @Date: 2021/8/17
  9.  * @Description: 是否删除
  10.  */
  11. //不使用全局配置可使用该注解
  12. //@JsonSerialize(using = BaseEnumSerializer.class)
  13. public enum DeleteEnum implements IBaseEnum<Boolean> {

  14.     DELETE_YES(true,"已删除"),
  15.     DELETE_NO(false,"未删除");

  16.     //使用JsonValue注解可格式化显示值
  17.     @JsonValue
  18.     private final boolean id;
  19.     private final String name;

  20.     DeleteEnum(boolean id, String name) {
  21.         this.id = id;
  22.         this.name = name;
  23.     }

  24.     @Override
  25.     public Boolean getValue() {
  26.         return this.id;
  27.     }

  28.     @Override
  29.     public String getName() {
  30.         return this.name;
  31.     }

  32.     //使用JsonValue注解可注释掉toString
  33.     /*@Override
  34.     public String toString() {
  35.         //根据情况给前台数据展示deleted或descp
  36.         return String.valueOf(this.deleted);
  37.     }*/
  38. }


阅读(1091) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~