Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将参数的类传递给另一个方法调用?_Java_Generics_Jax Rs_Cxf - Fatal编程技术网

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

Java 如何将参数的类传递给另一个方法调用?,java,generics,jax-rs,cxf,Java,Generics,Jax Rs,Cxf,我正在创建一个方法,该方法将调用,如下所示 public T create(Class<T> resourceType) throws Exception { Class<T> resource = JAXRSClientFactory.create(basePath, resourceType.getClass(), username, password, null); // do common configuration for any interfa

我正在创建一个方法,该方法将调用,如下所示

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();
}
public T create(类resourceType)引发异常{
Class resource=JAXRSClientFactory.create(basePath,resourceType.getClass(),用户名,密码,null);
//对任何接口T进行公共配置
Client Client=WebClient.Client(资源);
WebClient WebClient=WebClient.fromClient(客户端);
SSLUtil.configure(网络客户端);
返回resource.newInstance();
}
但是,这并没有像我预期的那样工作,因为
resourceType.getClass()
返回
java.lang.Class
,而不是
t
的类

将对
JAXRSClientFactory
的调用改为使用
resourceType
将导致以下不兼容类型错误:


如何修改此方法,以便将
T
类传递给
JAXRSClientFactory.create()

resourceType.getClass()
将始终返回
结果,而不是
资源类型。getClass()
将始终返回
结果,与
相反,您似乎认为参数
类resourceType
是T的一个实例。这是错误的

Class resourceType
的参数定义没有将
resourceType
定义为
T
的实例,而是定义为
T
的类(即
Class
的实例,即
T.Class

因此,因为
resourceType
是类的实例,所以调用
resourceType.getClass()
返回
class.class

如果希望参数是T的实例,则需要方法签名为:

public T create(T resourceType)
但实际上,我怀疑您可能确实想要类,而不是实例,因此您应该将调用更改为
JAXRSClientFactory.create
以:

T resource = JAXRSClientFactory.create(basePath, resourceType, username, password, null);

您似乎认为参数
Class resourceType
是T的一个实例。这是错误的

Class resourceType
的参数定义没有将
resourceType
定义为
T
的实例,而是定义为
T
的类(即
Class
的实例,即
T.Class

因此,因为
resourceType
是类的实例,所以调用
resourceType.getClass()
返回
class.class

如果希望参数是T的实例,则需要方法签名为:

public T create(T resourceType)
但实际上,我怀疑您可能确实想要类,而不是实例,因此您应该将调用更改为
JAXRSClientFactory.create
以:

T resource = JAXRSClientFactory.create(basePath, resourceType, username, password, null);
看看:

参数:

baseAddress
-baseAddress
cls
-代理类,如果不是接口,则将创建CGLIB代理
username
-用户名
密码
-密码
configLocation
-配置资源的类路径位置

返回:

类型化代理

您修改的代码:

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;
}
public T create(类resourceType)引发异常{
T resource=JAXRSClientFactory.create(basePath、resourceType、用户名、密码、null);
Client Client=WebClient.Client(资源);
WebClient WebClient=WebClient.fromClient(客户端);
SSLUtil.configure(网络客户端);
返回资源;
}
看看:

参数:

baseAddress
-baseAddress
cls
-代理类,如果不是接口,则将创建CGLIB代理
username
-用户名
密码
-密码
configLocation
-配置资源的类路径位置

返回:

类型化代理

您修改的代码:

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;
}
public T create(类resourceType)引发异常{
T resource=JAXRSClientFactory.create(basePath、resourceType、用户名、密码、null);
Client Client=WebClient.Client(资源);
WebClient WebClient=WebClient.fromClient(客户端);
SSLUtil.configure(网络客户端);
返回资源;
}

只需使用
resourceType
别担心,我不使用CXF,但看看API,我发现了您的问题,我认为您缺少API应该如何使用。factory类从接口创建代理。您可以使用接口进行调用。因此,您应该使用
resourceType
作为参数,并使用返回的代理进行调用。您不需要实例化类所有
JAXRSClientFactory.create
methods返回
t
,它是传递给它的类的实例。这就是你要做的代理调用。目前,您的方法没有产生任何CALL,我从未使用过CXF客户机,但我使用过RESTeasy客户机代理支持和Jersey客户机代理支持,这就是它们的工作方式。看看
JAXRSClientFactory
的文档,所有的方法都提到了代理的创建,所以我想它的工作原理完全相同,只是使用
resourceType
别担心,我不使用CXF,但是看看API,我看到了您的问题,我想您缺少了API应该如何使用。factory类从接口创建代理。您可以使用接口进行调用。因此,您应该使用
resourceType
作为参数,并使用返回的代理进行调用。您不需要实例化类所有
JAXRSClientFactory.create
methods返回
t
,它是传递给它的类的实例。这就是你要做的代理调用。目前,您的方法没有产生任何callI,我从未使用过CXF客户端,但我使用过RESTeasy客户端和