一:解决方法一
LocalDateTime——》DATETIME(中国标准时间) : 使用
to_date(#{executeTime},'yyyy-mm-dd hh24:mi:ss')
DATETIME(中国标准时间)——》LocalDateTime : 使用
to_char(#{executeTime},'yyyy-mm-dd hh24:mi:ss')

二:解决方法二
用@RequestBody 对象接受带有为LocalDateTime类型的属性,也会报错,那么可以这样解决
第一步:
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import org.springframework.boot.autoconfigure.jackson.JacksonProperties;
import org.springframework.boot.jackson.JsonComponent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper serializingObjectMapper(Jackson2ObjectMapperBuilder builder,
JacksonProperties jacksonProperties) {
ObjectMapper objectMapper = builder.build();
// 把“忽略重复的模块注册”禁用,否则下面的注册不生效
objectMapper.disable(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS);
// 自动扫描并注册相关模块
objectMapper.findAndRegisterModules();
// 手动注册相关模块
// objectMapper.registerModule(new ParameterNamesModule());
// objectMapper.registerModule(new Jdk8Module());
// objectMapper.registerModule(new JavaTimeModule());
// 重新设置为生效,避免被其他地方覆盖
objectMapper.enable(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS);
return objectMapper;
}
}
第二步:在实体类上添加注解
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")

文章提供了两种解决LocalDateTime与DATETIME类型转换的方法。第一种是通过SQL转换函数to_date和to_char进行转换。第二种是通过配置Jackson的ObjectMapper,利用JavaTimeModule处理LocalDateTime序列化问题,并在实体类上使用@JsonFormat注解指定日期格式。
5916

被折叠的 条评论
为什么被折叠?



