LocalDateTime不会转换为String而是转换为Json对象 [英] LocalDateTime is not converted to String but to Json object
问题描述
在调用spring-data-rest服务时,我想将java.time.LocalDateTime类型的创建日期序列化为String.实体的字段使用DateTimeFormat(iso = ISO.DATE_TIME)进行注释.我还在Spring Configuration类中注册了LocalData to String Converter.
I want to serialize a creation date of type java.time.LocalDateTime as String, when calling a spring-data-rest service. The field of the entity is annotated with DateTimeFormat(iso=ISO.DATE_TIME). I also registered a LocalData to String Converter in the Spring Configuration class.
@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);
}
}
但是不会调用该转换器,并且创建日期会序列化为Json结构,如下所示:
But the converter is not called and the creation date is serialized as a Json structure like this:
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"
}
这是实体定义:
@NodeEntity
public class CmmnElement {}
public class Definitions extends CmmnElement {
@DateTimeFormat(iso = ISO.DATE_TIME)
private LocalDateTime creationDate;
}
简单的存储库如下所示:
The simple repository looks like this:
@RepositoryRestResource(collectionResourceRel="definitions", path="/definitions")
public interface DefinitionsRepository extends PagingAndSortingRepository<Definitions, Long>{}
creationDate由LocalDateTime.parse()从此字符串"2011-12-03T10:15:30"创建.
The creationDate is created by LocalDateTime.parse() from this String "2011-12-03T10:15:30".
我在github上有一个示例: https://github .com/gfinger/neo4j-tests/tree/master/neo-spring-test Leaf类的LocalDateTime类型的creationDate.如果您运行mvn spring-boot:run,则使用Leaf实例初始化嵌入式Neoi4J DB.调用 http://localhost:8080/leaf%7B?page,size, sort} 将日期显示为json结构.
I have an example on github: https://github.com/gfinger/neo4j-tests/tree/master/neo-spring-test The Leaf class has a creationDate of type LocalDateTime. If you run mvn spring-boot:run the embedded Neoi4J DB gets initialized with a Leaf instance. A call to http://localhost:8080/leaf%7B?page,size,sort} shows the date as json structure.
如何获取此String而不是Json对象?
How can I get back this String instead of the Json object?
推荐答案
好的,抱歉.
我检查了一下,如果查看target/cmmn.db
中的Neo4j数据库,它表明日期已正确序列化为字符串.
Ok, sorry for the delay.
I checked it and if you look into the Neo4j database in target/cmmn.db
it shows that the date is correctly serialized as string.
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
我认为问题在于spring-data-rest的json-converters,可能未为LocalDateTime
配置.
I think the issue is rather with the json-converters of spring-data-rest, which might not be configured for LocalDateTime
.
您必须将其添加为依赖项:
You have to add this as dependency:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.4.5</version>
</dependency>
然后将叶子渲染为:
"name" : "Cato",
"description" : "Ceterum Censeo",
"creationDate" : "2011-12-03T10:15:30",
"_links" : {
"self" : {
"href" : "http://localhost:8080/leaf/7"
}
请参阅: 查看全文