Eclipse Java CXF 2.7.9 上的 Web 客户端 - 修复格式错误的 .wsdl 文件



我很难创建 WSDL 客户端,这要归功于其他人使用 Apache Axis 版本 1.4 创建的格式错误的 WSDL 定义。

让我向您展示我正在遵循的步骤:

首先,我在 SOAP IU 上加载我的 WSDL 端点 http://xxxxx/uglySoap?wsdl。程序自动生成以下请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bal="http://xxxx/uglySoap.xsd">
<soapenv:Header/>
<soapenv:Body>
<bal:CreditRequest>
<bal:MSISDN>?</bal:MSISDN>
<bal:amountToCredit>?</bal:amountToCredit>
<!--Optional:-->
<bal:reason>?</bal:reason>
<bal:transId>?</bal:transId>
</bal:CreditRequest>
</soapenv:Body>
</soapenv:Envelope>

此请求是错误的,因为它缺少标头(您提供凭据的位置(。在 SOAP UI 上,这不是问题,我可以手动添加缺少的文本,它将完美运行。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:msp="http:xxxxxx/uglySoap.xsd" xmlns:bal="http://xxxxxx/uglySoap.xsd">
<soapenv:Header>
<msp:messageHeader>
<msp:trackingMessageHeader>
<msp:messageId>xxx</msp:messageId>
<msp:carrierId>xxx</msp:carrierId>
<msp:userId>xxxx</msp:userId>
<msp:password>xxxx</msp:password>            
</msp:trackingMessageHeader>
</msp:messageHeader>
</soapenv:Header>
<!--from here is the same thing as before-->
<soapenv:Body>
<bal:CreditRequest>
<bal:MSISDN>xxxxx</bal:MSISDN>
<bal:amountToCredit>xxxx</bal:amountToCredit>
<!--Optional:-->
<bal:reason>xxxx</bal:reason>
<bal:transId>xxxx</bal:transId>
</bal:CreditRequest>
</soapenv:Body>
</soapenv:Envelope>

真正的痛苦始于我尝试在 Java 上使用 Web 服务。Eclipse CXF 2.7.9。工具将毫无困难地导入拙劣的 WSDL 版本,但调用其方法是无用的,因为它们很好......拙劣。

creditResponse = balanceManager.getBalanceManagement((.applyCredit(creditRequest, authHeader(;

JAVA 错误:方法 credit(CreditRequest, MessageHeader( 未定义类型为 BalanceManagement 类型。真的是老兄?你已经知道这会发生。

所以。。。

  • 我尝试手动编辑@WebMethod条目以包含缺少的功能。那失败了。
  • 我尝试(几个小时(创建一个本地版本的uglySoap.wsdl,包括丢失的标头,然后将其导入Java,但它抛出cryptic org.apache.cxf.interceptor.ClientFaultConverter.processFaultDetail错误。
  • 我什至尝试切换到安讯士,但无济于事。

拜托,有没有针对 Java 的解决方案,我可以像 SOAP UI 一样打入我的 SOAP 请求、URL,并以.XML文件的形式获取响应(甚至作为纯文本,没问题!(?

谢谢!

好的伙计们,我找到了一个可行的解决方案来手动提出请求。

创建一个新的 Maven 项目:

package main;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Assert;
import io.restassured.path.xml.XmlPath;
public class Client {
public void executeRequest() {
try
{
CloseableHttpClient client = HttpClients.createDefault(); // create client
HttpPost request = new HttpPost("http://172.27.241.11:8195/mspgwservices/services/BalanceManagement"); // Create
                                              // the
String requestXmlText = "<soapenv:Envelope ...INCLUDE YOUR XML REQUEST HERE, ON THE SAME WAY YOU WOULD DO IT ON SOAP UI... </soapenv:Envelope>";
StringEntity stringEntity = new StringEntity(requestXmlText, "utf-8");
request.addHeader("soapAction", "Credit");
request.addHeader("Accept", "text/xml");
request.addHeader("Content-Type", "text/xml;charset=utf-8");
request.setEntity(stringEntity);
CloseableHttpResponse response = client.execute(request);// Execute the command
int statusCode = response.getStatusLine().getStatusCode();
// Get the status code and assert
System.out.println("Status code: " + statusCode);
Assert.assertEquals(200, statusCode);
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
// Getting the Response body
System.out.println(responseString);
XmlPath jsXpath = new XmlPath(responseString);
String textResult = jsXpath.getString("respDescription");
System.out.println("result: " + textResult );
}
catch (Exception ex)
{
System.out.println("Error." + ex.toString());
}
}
}

另外,不要忘记将以下行添加到POM中.XML以便导入所有必需的库

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>BalanceadorLineas</groupId>
<artifactId>BalanceadorLineas</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-restassured</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.scala-js</groupId>
<artifactId>scalajs-junit-test-runtime_2.11</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</project>

痛苦终于结束了!希望它也适合您。

最新更新