LocalDateTime不会转换为String而是转换为Json对象

时间:2015-06-16 11:43:08

标签: spring neo4j spring-data spring-data-neo4j spring-data-rest

我想在调用spring-data-rest服务时将java.time.LocalDateTime类型的创建日期序列化为String。实体的字段使用DateTimeFormat(iso = ISO.DATE_TIME)进行注释。我还在Spring Configuration类中注册了LocalData to String Converter。

@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结构:

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;
}

简单存储库如下所示:

@RepositoryRestResource(collectionResourceRel="definitions", path="/definitions")
public interface DefinitionsRepository extends PagingAndSortingRepository<Definitions, Long>{}

creationDate由LocalDateTime.parse()从此String&#34; 2011-12-03T10:15:30&#34;。

创建。

我在github上有一个例子:https://github.com/gfinger/neo4j-tests/tree/master/neo-spring-test Leaf类有一个类型为LocalDateTime的creationDate。如果你运行mvn spring-boot:运行嵌入式Neoi4J DB会被一个Leaf实例初始化。对http://localhost:8080/leaf%7B?page,size,sort}的调用将日期显示为json结构。

如何取回此String而不是Json对象?

2 个答案:

答案 0 :(得分:1)

好的,抱歉延误了。 我查了一下,如果你查看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

我认为问题在于spring-data-rest的json转换器,它可能没有为LocalDateTime配置。

您必须将其添加为依赖项:

    <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"
    }

请参阅:https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring#customizing-the-jackson-objectmapper

答案 1 :(得分:0)

我在Spring MVC中有类似的问题。添加jackson-datatype-jsr310的依赖关系并不能解决我的问题,而是由自定义格式化程序完成了该工作。

我在这里发现https://stackoverflow.com/a/34548741/9091816

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);
    }
}

,然后在LocalDateTime字段中使用该自定义序列化程序:

@JsonSerialize(using = CustomLocalDateTimeSerializer.class)
private LocalDateTime creationDate;