Eclipse Java CXF 2.7.9 上的 Webclient - 修复格式错误的 .wsdl 文件
Webclient on Eclipse Java CXF 2.7.9 - Fixing a malformed .wsdl file
我很难创建 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>
此请求是错误的,因为它缺少 header(您提供凭据的地方)。在 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 错误:BalanceManagement 类型未定义方法 credit(CreditRequest, MessageHeader)。真的吗伙计?你已经知道这会发生。
所以...
- 我尝试手动编辑@WebMethod 条目以包含缺少的功能。那失败了。
- 我尝试(花了几个小时)创建 uglySoap.wsdl 的本地版本,包括丢失的 header,然后将其导入 Java,但它抛出神秘的 org.apache.cxf.interceptor.ClientFaultConverter.processFaultDetail 错误。
- 我什至尝试切换到 Axis,但无济于事。
请问,对于 Java 是否有任何解决方案,我可以在其中输入我的 SOAP 请求 URL,并将响应作为 .XML 文件(或者甚至作为纯文本,没问题!)就像 SOAP UI 一样?
谢谢!
好的,我找到了一个可行的解决方案来手动发出请求。
创建一个新的 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>
痛苦终于过去了!希望对你也有用。
我很难创建 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>
此请求是错误的,因为它缺少 header(您提供凭据的地方)。在 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 错误:BalanceManagement 类型未定义方法 credit(CreditRequest, MessageHeader)。真的吗伙计?你已经知道这会发生。
所以...
- 我尝试手动编辑@WebMethod 条目以包含缺少的功能。那失败了。
- 我尝试(花了几个小时)创建 uglySoap.wsdl 的本地版本,包括丢失的 header,然后将其导入 Java,但它抛出神秘的 org.apache.cxf.interceptor.ClientFaultConverter.processFaultDetail 错误。
- 我什至尝试切换到 Axis,但无济于事。
请问,对于 Java 是否有任何解决方案,我可以在其中输入我的 SOAP 请求 URL,并将响应作为 .XML 文件(或者甚至作为纯文本,没问题!)就像 SOAP UI 一样?
谢谢!
好的,我找到了一个可行的解决方案来手动发出请求。
创建一个新的 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>
痛苦终于过去了!希望对你也有用。