【问题标题】:Use LocalDateTime wirh Spring Boot and Gson将 LocalDateTime 与 Spring Boot 和 Gson 结合使用
【发布时间】:2022-11-30 04:41:22
【问题描述】:

我有一个简单的 maven 项目,用于从 open-api json 文件生成带有 openapi-generator-maven-plugin 的休息客户端。

我正在使用<library>okhttp-gson</library>,因为我不想使用OffsetDateTime,所以我也在使用<dateLibrary>java8-localdatetime</dateLibrary>

为了构建生成的源代码,我使用了这些依赖项

<properties>
    <gson-version>2.10</gson-version>
    <gson-fire-version>1.8.5</gson-fire-version>
    <okhttp3-version>4.10.0</okhttp3-version>
    <swagger-version>1.6.8</swagger-version>
</properties>

<dependencies>
    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.1.1</version>
    </dependency>
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>${swagger-version}</version>
    </dependency>
    <!-- @Nullable annotation -->
    <dependency>
        <groupId>com.google.code.findbugs</groupId>
        <artifactId>jsr305</artifactId>
        <version>3.0.2</version>
    </dependency>
    <!-- HTTP client : okhttp3 -->
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>logging-interceptor</artifactId>
        <version>${okhttp3-version}</version>
    </dependency>
    <!-- JSON processing : gson -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.10</version>
    </dependency>
    <dependency>
        <groupId>io.gsonfire</groupId>
        <artifactId>gson-fire</artifactId>
        <version>${gson-fire-version}</version>
    </dependency>
</dependencies>

接下来我有一个使用客户端的 Spring Boot 项目。

    <dependency>
        <groupId>com.example</groupId>
        <artifactId>demo-api-client</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

问题是当我使用客户端时

java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.time.LocalDate java.time.LocalDateTime.date accessible: module java.base does not "opens java.time" to unnamed module @562ff1d6
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354) ~[na:na]
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) ~[na:na]
...

我在某处阅读以添加

@Configuration
public class GsonConfiguration {

    @Bean
    public GsonBuilderCustomizer typeAdapterRegistration() {
        System.out.println("---typeAdapterRegistration");
        return builder -> {
            builder.registerTypeAdapter(LocalDateTimeDeserializer.class, new LocalDateTimeDeserializer());
        };
    }

}

public class LocalDateTimeDeserializer implements JsonDeserializer<LocalDateTime> {

    @Override
    public LocalDateTime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        System.out.println("---deserialize");
        return LocalDateTime.parse(jsonElement.getAsString(), DateTimeFormatter.ISO_LOCAL_DATE);
    }

}

但不是不工作。 typeAdapterRegistration 已执行,但未执行 deserialize

我能怎么做 ?

【问题讨论】:

  • 该错误提示 Java9+,但您添加了一个 Java 8 库。 LocalDateTime 已从 Joda 移至 java base,因此您在那里不匹配。
  • 我不明白。包 java.time.* 是在 java 8 (jsr310) 中引入的。不 ?我在使用 joda 库的任何地方都看不到。
  • 对不起。我想我有点太累了......我的眼睛在欺骗我......你需要在运行应用程序时添加--add-opens java.base/java.time=ALL-UNNAMED

标签: spring-boot gson localdatetime


【解决方案1】:

很可能是以下行导致了问题:

builder.registerTypeAdapter(LocalDateTimeDeserializer.class, new LocalDateTimeDeserializer())

这里你注册了LocalDateTimeDeserializer类型的反序列化器,但是你不想反序列化一个LocalDateTimeDeserializer,你想反序列化一个LocalDateTime,所以它应该是:

builder.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeDeserializer())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-01
    • 2017-10-28
    • 2017-03-30
    • 2019-09-24
    • 2014-02-20
    • 2023-02-09
    • 1970-01-01
    相关资源
    最近更新 更多