Retrofit2,maven项目,非法反射访问警告

Retrofit2, maven project, Illegal reflective access warning

刚开始新建maven项目,简单实现了Retrofit客户端。我收到以下警告:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by retrofit2.Platform (file:/C:/Users/Admin/.m2/repository/com/squareup/retrofit2/retrofit/2.8.1/retrofit-2.8.1.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)
WARNING: Please consider reporting this to the maintainers of retrofit2.Platform
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Process finished with exit code 0

这是代码

import retrofit2.Retrofit;
import retrofit2.SimpleXmlConverterFactory;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

public class RetrofitClient {

    private static Retrofit retrofit = null;
    private RetrofitClient() { }

    public static EndPoints getAPI(String baseUrl) {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(SimpleXmlConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
        }
        return retrofit.create(EndPoints.class);
    }
}

界面很简单

import retrofit2.Call;
import retrofit2.http.GET;

public interface EndPoints {
    @GET("teststatuses")
    Call<String> testStatus();
}

调用看起来像这样:

EndPoints endPoints = RetrofitClient.getAPI("http://localhost:8080/");
Call<String> repos = endPoints.testStatus();
System.out.println(repos.execute());

该项目使用 SDK 11java 语言级别 11

今天我遇到了同样的错误。我将 Retrofit 2.8.1 导入到我的项目中,它出现了。我尝试了所有方法,但有一件事有所帮助 - 我将 Retrofit 版本更改为 2.7.2,现在一切正常。祝你好运!) 如果你使用maven:

<dependency>
    <groupId>com.squareup.retrofit2</groupId>
    <artifactId>retrofit</artifactId>
    <version>2.7.2</version>
</dependency>

Gradle:

compile group: 'com.squareup.retrofit2', name: 'retrofit', version: '2.7.2'

有一篇关于此的 issue 文件,其中一位 Retrofit 维护者回应:

The reflection works around a bug in the JDK which was fixed in 14 but it's only used for default methods. As it's only a warning, it's not preventing your call from working.

所以你的选择是

  1. 坚持改造 2。8.x,以及
    • 忽略警告,或者
    • 升级到Java14
  2. 降级到 Retrofit 2.7。*

如果您在 Java 版本 9 和 13 之间,并且想坚持使用 2.8 或更高版本的 Retrofit,您可以 运行 您的 jar 文件,如下所示:

java --add-opens=java.base/java.lang.invoke=ALL_UNNAMED my_jar.jar

请注意,这不适用于 Java 8,因此如果您真的需要,您可以破解这样的解决方案(适用于 *nix):

if java --add-opens 2>&1 | grep 'requires modules' >/dev/null; then
  java --add-opens=java.base/java.lang.invoke=ALL-UNNAMED -jar my_jar.jar
else
  java -jar my_jar.jar
fi

如果您有兴趣进一步研究,可以查看 the code that causes this

我还发现这有一些很好的信息:https://blog.codefx.org/java/five-command-line-options-hack-java-module-system/

如果您正在使用 Gradle,您还可以尝试添加默认的 jvm args:。但是,这仍然无法为您提供可以正常工作的普通 jar 文件。

使用版本时要小心。 降级到 Retrofit 2.7.* 为我解决了这个问题

我已将此块添加到我的模块 build.gradle 文件中。

Gradle

tasks.withType(Test) {
  /**
   * fix for retrofit https://github.com/square/retrofit/issues/3341
   */
  jvmArgs = ["--add-opens", "java.base/java.lang.invoke=ALL-UNNAMED"]
}

Kotlin DSL

tasks.withType<Test> {
  /**
   * fix for retrofit https://github.com/square/retrofit/issues/3341
   */
  jvmArgs = listOf("--add-opens", "java.base/java.lang.invoke=ALL-UNNAMED")
}

警告不再显示。我认为以这种方式删除它是安全的,因为它只影响测试任务