From 34076294a7ba9169e2c895fd650551a22e02d0f5 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sat, 19 Apr 2025 02:01:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20JDK17+=20=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E4=B8=8B=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=20`Pagin?= =?UTF-8?q?gAndSortingQueryParamsTests#testGson`=20=E4=B8=8D=E9=80=9A?= =?UTF-8?q?=E8=BF=87=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 该用例在 JDK17+ 环境下使用 gson 进行序列化时,报 `com.google.gson.JsonIOException: Failed making field 'java.time.LocalDateTime#date' accessible; either increase its visibility or write a custom TypeAdapter for its declaring type`。 See: https://github.com/google/gson/blob/main/Troubleshooting.md#reflection-inaccessible --- .../PagingAndSortingQueryParamsTests.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/test/java/xyz/zhouxy/plusone/commons/model/dto/test/PagingAndSortingQueryParamsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/model/dto/test/PagingAndSortingQueryParamsTests.java index 2810acc..be0703b 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/model/dto/test/PagingAndSortingQueryParamsTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/model/dto/test/PagingAndSortingQueryParamsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -184,24 +184,34 @@ public class PagingAndSortingQueryParamsTests { assertThrows(IllegalArgumentException.class, () -> queryParams.buildPagingParams()); // NOSONAR } - static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); - @Test - void testGson() throws Exception { + void testGson() { Gson gson = new GsonBuilder() .registerTypeAdapter(LocalDate.class, new TypeAdapter() { @Override public void write(JsonWriter out, LocalDate value) throws IOException { - out.value(dateFormatter.format(value)); + out.value(DateTimeFormatter.ISO_DATE.format(value)); } @Override public LocalDate read(JsonReader in) throws IOException { - return LocalDate.parse(in.nextString(), dateFormatter); + return LocalDate.parse(in.nextString(), DateTimeFormatter.ISO_DATE); } }) + .registerTypeAdapter(LocalDateTime.class, new TypeAdapter() { + + @Override + public void write(JsonWriter out, LocalDateTime value) throws IOException { + out.value(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(value)); + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + return LocalDateTime.parse(in.nextString(), DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + }) .create(); try (SqlSession session = sqlSessionFactory.openSession()) { AccountQueryParams params = gson.fromJson(JSON_STR, AccountQueryParams.class); @@ -212,6 +222,7 @@ public class PagingAndSortingQueryParamsTests { List list = accountQueries.queryAccountList(params, pagingParams); long count = accountQueries.countAccount(params); PageResult accountPageResult = PageResult.of(list, count); + log.info(gson.toJson(accountPageResult)); assertEquals(Lists.newArrayList( @@ -226,7 +237,7 @@ public class PagingAndSortingQueryParamsTests { } AccountQueryParams queryParams = gson.fromJson(WRONG_JSON_STR, AccountQueryParams.class); - assertThrows(IllegalArgumentException.class, () -> queryParams.buildPagingParams()); // NOSONAR + assertThrows(IllegalArgumentException.class, queryParams::buildPagingParams); } }