@JsonSerialize(using = LocalDateTimeSerializer.
class
)
@JsonDeserialize(using = LocalDateTimeDeserializer.
class
)
@JsonFormat(pattern =
"yyyy-MM-dd HH:mm:ss.SSS"
)
private
LocalDateTime createTime
;
我目前开发的项目SpringBoot版本比较低,也遇到了类似问题,找到的解决方案是Application里面设置一个Bean
@Bean
public ObjectMapper serializingObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.registerModule(new JavaTimeModule());
return objectMapper;
希望可以对你有参考。
config 里定义了 ObjectMapper bean,feign调用时没作用,改用了重写feignclien的encoder来暂时实现
@Bean
public ObjectMapper serializingObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
//禁止将Date序列化为时间戳
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// objectMapper.registerModule(new JavaTimeModule());
objectMapper.registerModule(new Jdk8Module());
JavaTimeModule module = new JavaTimeModule();
// 时间格式
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
module.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
module.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
// long 转 string
module.addSerializer(Long.class, ToStringSerializer.instance);
module.addSerializer(Long.TYPE, ToStringSerializer.instance);
objectMapper.registerModule(module);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return objectMapper;
class CustomEncoder implements Encoder {
@Override
public void encode(Object o, Type type, RequestTemplate requestTemplate) throws EncodeException {
SpringEncoder bean = SpringContextUtil.getBean(SpringEncoder.class);
bean.encode(o, type, requestTemplate);
ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule module = new JavaTimeModule();
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
objectMapper.registerModule(module);
try {
String body = objectMapper.writeValueAsString(o);
requestTemplate.body(body);
} catch (JsonProcessingException e) {
e.printStackTrace();