gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:10:28 29 4
gpt4 key购买 nike

我刚刚开始一个新的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 11运行Java语言级别11

最佳答案

有一个issue归档,改造维护者之一对此做出了回应:

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.



因此,您可以选择
  • 坚持使用Retrofit 2.8.x,并且
  • 忽略警告,或
  • 升级到Java 14
  • 降级为Retrofit 2.7。*
  • 关于java - Retrofit2,maven项目,非法反射访问警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60915381/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com