Soap 服务是远程托管的,但由于我们服务器的一些限制,我获得了 WSDL 并将其存盘在本地。使用 maven、org.apache.cxf 插件和生成源命令,我从本地 WSDL 生成了类。
WebServiceClient 类属性:
@WebServiceClient(name = "ManageCredit",
wsdlLocation = localWSDLAddress,
targetNamespace = targetNamespace)
public class ManageCredit extends Service {
public final static URL WSDL_LOCATION;
public final static QName SERVICE = new QName(targetNamespace, "ManageCredit");
public final static QName ManageCreditEndpoint = new QName(targetNamespace, "ManageCreditEndpoint");
static {
URL url = null;
try {
url = new URL(localWSDLAddress);
} catch (MalformedURLException e) {
java.util.logging.Logger.getLogger(ManageCredit.class.getName())
.log(java.util.logging.Level.INFO,
"Can not initialize the default wsdl from {0}", localWSDLAddress);
}
WSDL_LOCATION = url;
}
客户端设定:
ManageCredit manageCreditService = new ManageCredit();
ManageCreditPortType manageCreditPortType = manageCreditService.getManageCreditEndpoint();
SignContractRequest signContractRequest = new SignContractRequest();
signContractRequest.setRequestID("123");
signContractRequest.setApplicationID("1111");
signContractRequest.setChannelID("someValue");
manageCreditPortType.signContract(signContractRequest);
使用此代码,呼叫 http://localhost:8080/.... 我需要呼叫远程服务器,例如https://example.google:1820,而不是 localhost。如何在不更改 wsdl 位置的情况下更改客户端呼叫的端点。
uj5u.com热心网友回复:
由于您在本地下载了 WSDL,因此您现在可以控制它。因此,如果您将 WSDL 档案本身中的 SOAP 地址更改为指向您想要从客户端呼叫的任何地址,它可能会起作用。
或者,您可以尝试使用此处BindingProvider.ENDPOINT_ADDRESS_PROPERTY
所示的方法,但我记得您需要为每个请求执行此操作,而不是为整个服务客户端配置它:
MyService service = new MyService(wsdlURL, serviceName);
ServicePort client = service.getServicePort();
BindingProvider provider = (BindingProvider)client;
// You can set the address per request here
provider.getRequestContext().put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
"http://my/new/url/to/the/service");
uj5u.com热心网友回复:
您应该将 wsdl 档案复制到 src/main/resources 档案夹下的某个位置,以便将其打包在项目的 jar 档案中(对于下面的示例,我使用 src/main/resources/META-INF/wsdl)。配置cxf-codegen-plugin时,指定wsdlLocation配置值,如下图。
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>my-wsdl</id>
<phase>generate-sources</phase>
<goals>
<goal>wsdl2java</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<wsdlOptions>
<wsdlOption>
<wsdl>${project.basedir}\src\main\resources\META-INF\wsdl\helloWorld.wsdl</wsdl>
<wsdlLocation>classpath:/META-INF/wsdl/helloWorld.wsdl</wsdlLocation>
</wsdlOption>
</wsdlOptions>
</configuration>
</execution>
</executions>
</plugin>
您生成的 WebServiceClient 代码应该然后从类路径加载 wsdl,类似于这个...
@WebServiceClient(name = "HelloWorldService",
wsdlLocation = "classpath:/META-INF/wsdl/helloWorld.wsdl",
targetNamespace = "http://example.com/helloWorld")
public class HelloWorldService extends Service {
public final static URL WSDL_LOCATION;
public final static QName SERVICE = new QName("http://example.com/helloWorld", "HelloWorldService");
public final static QName HelloWorldProxyPort = new QName("http://example.com/helloWorld", "HelloWorldProxyPort");
static {
URL url = HelloWorldService.class.getClassLoader().getResource("/META-INF/wsdl/helloWorld.wsdl");
if (url == null) {
url = HelloWorldService.class.getClassLoader().getResource("META-INF/wsdl/helloWorld.wsdl");
}
if (url == null) {
java.util.logging.Logger.getLogger(HelloWorldService.class.getName())
.log(java.util.logging.Level.INFO,
"Can not initialize the default wsdl from {0}", "classpath:/META-INF/wsdl/helloWorld.wsdl");
}
WSDL_LOCATION = url;
}
...
}
使用服务时,可以如下设定服务的端点地址...
HelloWorldService helloWorldService = new HelloWorldService();
HelloWorldPort client = service.getHelloWorldPort();
BindingProvider provider = (BindingProvider)client;
// You can set the address per request here
provider.getRequestContext().put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
"http://my/new/url/to/the/service");
0 评论