解决SpringMVC 返回Java8 时间JSON数据的格式化问题处理

  /**

  * json处理工具类

  *

  *

  */

  @Component

  public class JsonUtil {

  private static final ObjectMapper mapper;

  public ObjectMapper getMapper() {

  return mapper;

  }

  static {

  mapper = new ObjectMapper();

  SimpleModule module = new SimpleModule();

  module.addSerializer(LocalDate.class, new LocalDateSerializer());

  module.addSerializer(LocalTime.class, new LocalTimeSerializer());

  module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());

  mapper.registerModule(module);

  }

  public static String toJson(Object obj) {

  try {

  return mapper.writeValueAsString(obj);

  } catch (Exception e) {

  throw new RuntimeException("转换json字符失败!");

  }

  }

  public T toObject(String json, Class clazz) {

  try {

  return mapper.readValue(json, clazz);

  } catch (IOException e) {

  throw new RuntimeException("将json字符转换为对象时失败!");

  }

  }

  }

  class LocalDateSerializer extends JsonSerializer {

  private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

  @Override

  public void serialize(LocalDate value, JsonGenerator jgen, SerializerProvider provider)

  throws IOException, JsonProcessingException {

  jgen.writeString(dateFormatter.format(value));

  }

  }

  class LocalDateTimeSerializer extends JsonSerializer {

  private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

  @Override

  public void serialize(LocalDateTime value, JsonGenerator jgen, SerializerProvider provider)

  throws IOException, JsonProcessingException {

  jgen.writeString(dateTimeFormatter.format(value));

  }

  }

  class LocalTimeSerializer extends JsonSerializer {

  private static final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");

  @Override

  public void serialize(LocalTime value, JsonGenerator jgen, SerializerProvider provider)

  throws IOException, JsonProcessingException {

  jgen.writeString(timeFormatter.format(value));

  }

  }