LocalDateTime不会被转换为字符串,而是转换为Json对象。

13 浏览
0 Comments

LocalDateTime不会被转换为字符串,而是转换为Json对象。

当调用spring-data-rest服务时,我想将类型为java.time.LocalDateTime的创建日期序列化为字符串。实体的字段被注解为@DateTimeFormat(iso=ISO.DATE_TIME)。我还在Spring配置类中注册了一个LocalData到String的转换器。

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

简单的repository如下:

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

创建日期通过LocalDateTime.parse()方法从字符串"2011-12-03T10:15:30"创建。

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

如何获取这个字符串而不是Json对象?

0
0 Comments

问题的出现原因是Spring Data REST的json转换器可能没有为LocalDateTime进行配置。解决方法是添加com.fasterxml.jackson.datatype:jackson-datatype-jsr310依赖,并进行相关配置。

在Neo4j数据库中,日期被正确地序列化为字符串。但是当从自定义的RestControllers返回时,我的自定义转换器仍然被忽略了。这是否需要对转换服务进行其他配置?

0
0 Comments

问题的原因是在Spring MVC中,当将LocalDateTime对象转换为JSON对象时,它并不会直接转换为字符串,而是转换为一个JSON对象。添加jackson-datatype-jsr310依赖并不能解决这个问题,而是需要使用自定义的格式化器来解决。

解决方法是创建一个自定义的LocalDateTime序列化器,将LocalDateTime对象格式化为指定格式的字符串,并在需要转换的字段上使用该自定义序列化器。以下是一个示例的自定义序列化器的代码:

public class CustomLocalDateTimeSerializer extends JsonSerializer{
    private static final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
    public void serialize(LocalDateTime dateTime, JsonGenerator generator, SerializerProvider sp)
            throws IOException, JsonProcessingException {
        String formattedDateTime = dateTime.format(DateTimeFormatter.ofPattern(dateTimeFormat)); 
        generator.writeString(formattedDateTime);
    }
}

然后,在需要转换的LocalDateTime字段上使用该自定义序列化器:

(using = CustomLocalDateTimeSerializer.class)
private LocalDateTime creationDate;

通过使用自定义序列化器,我们可以将LocalDateTime对象转换为指定格式的字符串,从而解决将其转换为JSON对象时的问题。

0