相关疑难解决方法(0)

LocalDate 序列化:日期作为数组?

我使用 Java 11 并希望将 LocalDate/LocalDateTime 序列化/反序列化为字符串。好的。我添加了依赖:

    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>${jackson.version}</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

和模块:

@Bean
public ObjectMapper objectMapper() {
    return new ObjectMapper()
            .registerModule(new JavaTimeModule())
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
            .enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
}
Run Code Online (Sandbox Code Playgroud)

当我将日期发送到我的应用程序时,它会正确反序列化:

{"profileId":12608,"birthDate":"2008-03-20","relativeType":"SON","cohabitants":true}
Run Code Online (Sandbox Code Playgroud)

当我直接使用 objectMapper 作为 bean 时,它也可以正确序列化:

{"code":"SUCCESS","id":868,"profileId":12608,"birthDate":"2008-03-20","relativeType":"SON","cohabitants":true}
Run Code Online (Sandbox Code Playgroud)

但是当它与控制器序列化时,它序列化为数组:

{"code":"SUCCESS","id":868,"profileId":12608,"birthDate":[2008,3,20],"relativeType":"SON","cohabitants":true}
Run Code Online (Sandbox Code Playgroud)

问题是在控制器上反序列化正文中的日期。控制器是:

@PostMapping
public Relative create(@Validated(Validation.Create.class) @RequestBody Relative relative) {
    return service.create(relative);
}
Run Code Online (Sandbox Code Playgroud)

相对类:

@Getter
@Setter
@ToString(callSuper = true)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Relative extends MortgageResponse {

    @Null(groups = Validation.Create.class)
    @NotNull(groups = Validation.Update.class)
    private Long id;

    @NotNull
    private Long profileId;

    private LocalDate birthDate;
    private RelativeType relativeType; …
Run Code Online (Sandbox Code Playgroud)

java spring objectmapper

12
推荐指数
1
解决办法
1万
查看次数

Jackson 将 Instant 序列化为纳秒问题

Jackson串行化java.time.InstantWRITE_DATE_TIMESTAMPS_AS_NANOSECONDS默认启用。

JSON是这样产生的

{ "timestamp":1421261297.356000000 }
Run Code Online (Sandbox Code Playgroud)

我想知道是否有办法摆脱最后的零。我想要这样的东西:

{ "timestamp":1421261297.356 }
Run Code Online (Sandbox Code Playgroud)

我试过:

mapper.configure( SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false );
mapper.configure( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true );
Run Code Online (Sandbox Code Playgroud)

但是此配置将其更改为毫秒表示1421261297356。我想要秒部分和小数毫秒部分。

java serialization json jackson java-time

5
推荐指数
1
解决办法
4882
查看次数

Jackson 使用 Java 8 将 elasticsearch 反序列化为 LocalDateTime

我们有一个用long弹性搜索索引填充的日期字段。

字段映射为:

@Field(type = FieldType.Date)
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
private LocalDateTime created;
Run Code Online (Sandbox Code Playgroud)

我使用Jackson JavaTimeModuleJdk8Module使用此配置:

@Bean
public ElasticsearchOperations elasticsearchTemplate() {
   return new ElasticsearchRestTemplate(client(), new CustomEntityMapper());
}

public static class CustomEntityMapper implements EntityMapper {

        private final ObjectMapper objectMapper;

        public CustomEntityMapper() {
            //we use this so that Elasticsearch understands LocalDate and LocalDateTime objects
            objectMapper = new ObjectMapper()
                              .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                              .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
                              .configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
                              .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
                              .configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
                              //MUST be registered BEFORE calling findAndRegisterModules
                              .registerModule(new JavaTimeModule())
                              .registerModule(new Jdk8Module());
            //only …
Run Code Online (Sandbox Code Playgroud)

java jackson elasticsearch spring-boot jackson-databind

3
推荐指数
1
解决办法
5124
查看次数

openapi java 生成器将 LocalDate 序列化为数组而不是完整日期

我在 spring 项目中使用 OpenAPI java 生成器 [1] 和 library:resttemplate, dateLibrary:java8 从规范生成客户端。

对于规范中的属性:

        targetDate:
          type: string
          format: date
Run Code Online (Sandbox Code Playgroud)

生成以下代码:

  public static final String JSON_PROPERTY_TARGET_DATE = "targetDate";

  private LocalDate targetDate;

  @javax.annotation.Nonnull
  @JsonProperty(JSON_PROPERTY_TARGET_DATE)
  @JsonInclude(value = JsonInclude.Include.ALWAYS)

  public LocalDate getTargetDate() {
    return targetDate;
  }

  @JsonProperty(JSON_PROPERTY_TARGET_DATE)
  @JsonInclude(value = JsonInclude.Include.ALWAYS)
  public void setTargetDate(LocalDate targetDate) {
    this.targetDate = targetDate;
  }
Run Code Online (Sandbox Code Playgroud)

我希望该字段能够序列化为完整日期,例如规范所承诺的“2023-01-01”:https: //spec.openapis.org/oas/v3.0.0#data-types。然而它实际上被序列化为一个数组:[2023,1,1]

同样的另一个属性

        otherDate:
          type: string
          format: date-time
Run Code Online (Sandbox Code Playgroud)

被序列化为自纪元以来的秒数,而不是全时。(我认为这是生成器中的错误)

由于生成了代码,我无法添加任何注释。我怎样才能确保日期正确序列化?

[1] openapi-generator-maven-plugin 6.3.0

java openapi jackson-databind openapi-generator openapi-generator-maven-plugin

2
推荐指数
1
解决办法
1905
查看次数