gpt4 book ai didi

java - 如何将参数的类传递给另一个方法调用?

转载 作者:行者123 更新时间:2023-12-01 16:54:07 26 4
gpt4 key购买 nike

我正在创建一个方法来调用 JAXRSClientFactory 如下图所示。

public T create(Class<T> resourceType) throws Exception {
Class<T> resource = JAXRSClientFactory.create(basePath, resourceType.getClass(), username, password, null);
// do common configuration for any interface T
Client client = WebClient.client(resource);
WebClient webClient = WebClient.fromClient(client);
SSLUtil.configure(webClient);
return resource.newInstance();
}

但是,这并没有像我预期的那样工作,因为 resourceType.getClass()返回java.lang.Class而不是T的类别。

将调用更改为 JAXRSClientFactory使用resourceType相反会导致以下不兼容类型错误:

incompatible types error screenshot

如何修改这个方法,以便我可以传递 T 的类至JAXRSClientFactory.create()

最佳答案

看看JAXRSClientFactory#create(String, Class, String, String, String) :

Parameters:

baseAddress - baseAddress
cls - proxy class, if not interface then a CGLIB proxy will be created
username - username
password - password
configLocation - classpath location of the configuration resource

Returns:

typed proxy

您修改后的代码:

public T create(Class<T> resourceType) throws Exception {
T resource = JAXRSClientFactory.create(basePath, resourceType, username, password, null);
Client client = WebClient.client(resource);
WebClient webClient = WebClient.fromClient(client);
SSLUtil.configure(webClient);
return resource;
}

关于java - 如何将参数的类传递给另一个方法调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35198016/

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