新建一个类,实现 WebMvcConfigurer 接口,重写 extendMessageConverters 方法,启动类上添加 @EnableAutoConfiguration 注解
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 org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 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 { @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { converters.removeIf(MappingJackson2HttpMessageConverter.class::isInstance); converters.add(0, new MappingJackson2HttpMessageConverter(objectMapper())); } @Bean public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); //忽略未知属性 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); //日期格式转换 mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); mapper.setTimeZone(TimeZone.getTimeZone("GMT+8")); //Long类型转String类型 SimpleModule simpleModule = new SimpleModule(); simpleModule.addSerializer(Long.class, ToStringSerializer.instance); simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance); mapper.registerModule(simpleModule); return mapper; } }
标签:mapper,SpringBoot,springframework,Long,org,import,annotation,丢失 From: https://www.cnblogs.com/langty/p/18591082