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");
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/388155.html
