Spring Boot不会为OffsetDateTime字段赋值

问题描述 投票:0回答:1

我有一个带有OffsetDateTime字段的模型:

public class Todo {
    @ApiModelProperty(required = true, value = "")
    @JsonProperty("creationTime")
    @Getter
    @Setter
    OffsetDateTime creationTime;

    @ApiModelProperty(required = true, value = "")
    @JsonProperty("title")
    @Getter
    @Setter
    String title;

}

Springboot总是为creationTime字段分配一个空值。如果我不提供ISO861格式的日期时间,我会得到解析异常。

但是,在实现@JsonComponent类之后,我可以看到分配的值。

@JsonComponent
public class OffsetDateSerializer {
    public static class OffSetDateDeserializer extends JsonDeserializer<OffsetDateTime> {

        @Override
        public OffsetDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
            System.out.println("Some");
            TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);

            TextNode textNode = (TextNode) treeNode;
            return OffsetDateTime.parse(textNode.asText());
        }
    }
}

这里发生了什么?

java spring-boot jackson
1个回答
0
投票

您应该配置OffsetDateSerializer的组件扫描路径。 JsonComponentModule(org.springframework.boot.jackson)注册使用@JsonComponent注释的json bean。

@Configuration
@ComponentScan("your OffsetDateSerializer's path")
public class SerializerConfig {

}
© www.soinside.com 2019 - 2024. All rights reserved.