Spring LocalDateTime未转换为字符串,而是转换为Json对象
我想在调用spring数据rest服务时,将java.time.LocalDateTime类型的创建日期序列化为字符串。实体的字段用DateTimeFormat(iso=iso.DATE\U TIME)注释。我还在Spring配置类中注册了一个LocalData到字符串转换器Spring LocalDateTime未转换为字符串,而是转换为Json对象,spring,neo4j,spring-data,spring-data-neo4j,spring-data-rest,Spring,Neo4j,Spring Data,Spring Data Neo4j,Spring Data Rest,我想在调用spring数据rest服务时,将java.time.LocalDateTime类型的创建日期序列化为字符串。实体的字段用DateTimeFormat(iso=iso.DATE\U TIME)注释。我还在Spring配置类中注册了一个LocalData到字符串转换器 @Override @Bean protected ConversionService neo4jConversionService() throws Exception { ConversionService c
@Override
@Bean
protected ConversionService neo4jConversionService() throws Exception {
ConversionService conversionService = super.neo4jConversionService();
ConverterRegistry registry = (ConverterRegistry) conversionService;
registry.removeConvertible(LocalDateTime.class, String.class);
registry.removeConvertible(String.class, LocalDateTime.class);
registry.addConverter(new DateToStringConverter());
registry.addConverter(new StringToDateConverter());
return conversionService;
}
private class DateToStringConverter implements Converter<LocalDateTime, String> {
@Override
public String convert(LocalDateTime source) {
return source.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
}
这是实体定义:
@NodeEntity
public class CmmnElement {}
public class Definitions extends CmmnElement {
@DateTimeFormat(iso = ISO.DATE_TIME)
private LocalDateTime creationDate;
}
简单存储库如下所示:
creationDate: {
dayOfYear: 337,
monthValue: 12,
hour: 10,
minute: 15,
second: 30,
nano: 0,
year: 2011,
month: "DECEMBER",
dayOfMonth: 3,
dayOfWeek: "SATURDAY",
chronology: {
id: "ISO",
calendarType: "iso8601"
}
@RepositoryRestResource(collectionResourceRel="definitions", path="/definitions")
public interface DefinitionsRepository extends PagingAndSortingRepository<Definitions, Long>{}
@RepositoryRestResource(collectionResourceRel=“definitions”,path=“/definitions”)
公共接口定义存储扩展分页和排序存储库{}
creationDate是由LocalDateTime.parse()从这个字符串“2011-12-03T10:15:30”创建的
我在github上有一个例子:Leaf类有一个LocalDateTime类型的creationDate。如果运行mvn spring boot:运行嵌入的Neoi4J DB,并使用叶实例初始化。调用将日期显示为json结构
如何取回这个字符串而不是Json对象?好的,很抱歉耽搁了。
我检查了它,如果您查看target/cmmn.db
中的Neo4j数据库,就会发现日期被正确地序列化为字符串
bin/neo4j-shell -path target/cmmn.db/
neo4j-sh (?)$ match (n) return *
> ;
+--------------------------------------------------------------------------------------+
| n |
+--------------------------------------------------------------------------------------+
| Node[0]{name:"Senat"} |
| Node[1]{name:"Cato",description:"Ceterum Censeo",creationDate:"2011-12-03T10:15:30"} |
+--------------------------------------------------------------------------------------+
2 rows
我认为问题在于SpringDataREST的json转换器,它可能没有为LocalDateTime
配置
您必须将其添加为依赖项:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.4.5</version>
</dependency>
请参阅:我在spring mvc中遇到了类似的问题。添加jackson-datatype-jsr310的依赖项并没有解决我的问题,相反,自定义格式化程序完成了这项工作 在这里我找到了
你的豆子看起来像什么?您存储的实例是什么?也许通用转换器优先?版本仍然是3.2.2。我用实体定义和存储的实例更新了帖子。你能顺便把它添加到你的示例项目中并再次共享链接吗?可能必须通过为什么Spring选择错误的序列化程序进行调试…一个问题可能是转换器是上下文无关的,例如Spring data rest可能会安装从LocalDateTime到JSON的转换器,这将超越您的持久性转换器…是的。我在GitHub上添加了一个示例。见上面。是的,太好了。对jackson-datatype-jsr310的依赖关系完成了spring数据rest存储库的工作。但是当从我自己的RESTController返回时,我的自定义转换器仍然被忽略。这是否需要转换服务的其他配置?
"name" : "Cato",
"description" : "Ceterum Censeo",
"creationDate" : "2011-12-03T10:15:30",
"_links" : {
"self" : {
"href" : "http://localhost:8080/leaf/7"
}
public class CustomLocalDateTimeSerializer extends JsonSerializer<LocalDateTime>{
private static final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
@Override
public void serialize(LocalDateTime dateTime, JsonGenerator generator, SerializerProvider sp)
throws IOException, JsonProcessingException {
String formattedDateTime = dateTime.format(DateTimeFormatter.ofPattern(dateTimeFormat));
generator.writeString( formattedDateTime);
}
}
@JsonSerialize(using = CustomLocalDateTimeSerializer.class)
private LocalDateTime creationDate;