添加依赖包
<!-- Jackson进行JSON序列化/反序列化添加Java 8的日期和时间库支持 --> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.13.0</version> </dependency>
方式一: 单个实体类
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
@JsonFormat(pattern = "HH:mm:ss",timezone = "GMT+8", shape = JsonFormat.Shape.STRING) @JsonDeserialize(using = LocalTimeDeserializer.class) // 反序列化 @JsonSerialize(using = LocalTimeSerializer.class) // 序列化 private LocalTime startTime; @JsonFormat(pattern = "HH:mm:ss",timezone = "GMT+8", shape = JsonFormat.Shape.STRING) @JsonDeserialize(using = LocalTimeDeserializer.class) // 反序列化 @JsonSerialize(using = LocalTimeSerializer.class) // 序列化 private LocalTime endTime;
方式二: 全局注入
/** * Copyright (c) 2018 储能系统 All rights reserved. * * http://www.avcon.com.cn * * 版权所有,侵权必究! */ package com.avcon.modules.security.config; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.avcon.common.utils.DateUtils; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.ByteArrayHttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.ResourceHttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.text.SimpleDateFormat; import java.util.List; import java.util.TimeZone; @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Bean public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); ObjectMapper mapper = new ObjectMapper(); //日期格式转换 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.setDateFormat(new SimpleDateFormat(DateUtils.DATE_TIME_PATTERN)); mapper.setTimeZone(TimeZone.getTimeZone("GMT+8")); //Long类型转String类型 JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(Long.class, ToStringSerializer.instance); javaTimeModule.addSerializer(Long.TYPE, ToStringSerializer.instance); mapper.registerModule(javaTimeModule); converter.setObjectMapper(mapper); return converter; } }
标签:Java,springframework,JSON,jackson,org,import,序列化,com,fasterxml From: https://www.cnblogs.com/eason-d/p/18088694