Webclient on Eclipse Java CXF 2.7.9 – 修复格式错误的 .wsdl 文件

huangapple go评论68阅读模式
英文:

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>

这个请求是错误的,因为它缺少头部(您在其中提供凭据)。在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>

   <!--从这里开始和之前一样-->

   <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的本地版本,然后将其导入到Java中,但它会抛出难以理解的org.apache.cxf.interceptor.ClientFaultConverter.processFaultDetail错误。
  • 我甚至尝试切换到Axis,也无济于事。

请问,是否有适用于Java的解决方案,我只需输入我的SOAP请求、URL,并将响应作为.XML文件(甚至纯文本,没有问题!)返回,就像SOAP UI一样?

谢谢!

英文:

I'm having a very hard time creating a WSDL client, thanks to a malformed WSDL definition created using Apache Axis version 1.4 by someone else.

Let me show you the steps I'm following:

First I load up my WSDL endpoint http://xxxxx/uglySoap?wsdl on SOAP IU. The program automatically generates the following Request:

&lt;soapenv:Envelope xmlns:soapenv=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:bal=&quot;http://xxxx/uglySoap.xsd&quot;&gt;
   &lt;soapenv:Header/&gt;
   &lt;soapenv:Body&gt;
      &lt;bal:CreditRequest&gt;
         &lt;bal:MSISDN&gt;?&lt;/bal:MSISDN&gt;
         &lt;bal:amountToCredit&gt;?&lt;/bal:amountToCredit&gt;
         &lt;!--Optional:--&gt;
         &lt;bal:reason&gt;?&lt;/bal:reason&gt;
         &lt;bal:transId&gt;?&lt;/bal:transId&gt;
      &lt;/bal:CreditRequest&gt;
   &lt;/soapenv:Body&gt;
&lt;/soapenv:Envelope&gt;

This request is wrong because it is missing the header (where you provide the credentials). On SOAP UI that's not a problem, I can add the missing text by hand and it will work perfectly.

&lt;soapenv:Envelope xmlns:soapenv=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:msp=&quot;http:xxxxxx/uglySoap.xsd&quot; xmlns:bal=&quot;http://xxxxxx/uglySoap.xsd&quot;&gt;
   &lt;soapenv:Header&gt;

          &lt;msp:messageHeader&gt;
         &lt;msp:trackingMessageHeader&gt;
            &lt;msp:messageId&gt;xxx&lt;/msp:messageId&gt;
            &lt;msp:carrierId&gt;xxx&lt;/msp:carrierId&gt;
            &lt;msp:userId&gt;xxxx&lt;/msp:userId&gt;
            &lt;msp:password&gt;xxxx&lt;/msp:password&gt;            
         &lt;/msp:trackingMessageHeader&gt;
      &lt;/msp:messageHeader&gt;
   &lt;/soapenv:Header&gt;

&lt;!--from here is the same thing as before--&gt;
   &lt;soapenv:Body&gt;
      &lt;bal:CreditRequest&gt;
         &lt;bal:MSISDN&gt;xxxxx&lt;/bal:MSISDN&gt;
         &lt;bal:amountToCredit&gt;xxxx&lt;/bal:amountToCredit&gt;
         &lt;!--Optional:--&gt;
         &lt;bal:reason&gt;xxxx&lt;/bal:reason&gt;
         &lt;bal:transId&gt;xxxx&lt;/bal:transId&gt;
      &lt;/bal:CreditRequest&gt;
   &lt;/soapenv:Body&gt;
&lt;/soapenv:Envelope&gt;

The real pain starts when I try to consume the web service on Java. The Eclipse CXF 2.7.9. tool will import the botched wsdl version without any trouble, but invoking its methods is useless since they are well... botched.

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

JAVA ERROR: The method credit(CreditRequest, MessageHeader) is undefined for the type BalanceManagement. Really dude? You already knew this was going to happen.

So...

  • I tried to manually edit the @WebMethod entries to include the missing features. That failed.
  • I tried (for hours) to create a local version of uglySoap.wsdl, include the missing header and then importing it to Java, but it throws cryptic org.apache.cxf.interceptor.ClientFaultConverter.processFaultDetail errors.
  • I even tried switching to Axis, to no avail.

Please, is there any solution for Java where I could just punch my SOAP request, the URL, and get the response as an .XML file (or even as plain text, no problem!) just as SOAP UI does?

Thank you!

答案1

得分: 0

好的,以下是您要求的代码部分的翻译:

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(); // 创建客户端
            HttpPost request = new HttpPost("http://172.27.241.11:8195/mspgwservices/services/BalanceManagement"); // 创建请求

            String requestXmlText = "<soapenv:Envelope ...在此处包括您的 XML 请求,与在 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); // 执行请求

            int statusCode = response.getStatusLine().getStatusCode();
            // 获取状态码并断言
            System.out.println("状态码:" + statusCode);

            Assert.assertEquals(200, statusCode);

            String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");

            // 获取响应主体
            System.out.println(responseString);

            XmlPath jsXpath = new XmlPath(responseString);
            String textResult = jsXpath.getString("respDescription");
            System.out.println("结果:" + textResult);

        } catch (Exception ex) {
            System.out.println("错误。" + 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>

最后,痛苦结束了!希望它对您也有效。

英文:

Ok guys, I found a working solution to make the requests by hand.

Create a new Maven project:

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(&quot;http://172.27.241.11:8195/mspgwservices/services/BalanceManagement&quot;); // Create
// the
String requestXmlText = &quot;&lt;soapenv:Envelope ...INCLUDE YOUR XML REQUEST HERE, ON THE SAME WAY YOU WOULD DO IT ON SOAP UI... &lt;/soapenv:Envelope&gt;&quot;;
StringEntity stringEntity = new StringEntity(requestXmlText, &quot;utf-8&quot;);
request.addHeader(&quot;soapAction&quot;, &quot;Credit&quot;);
request.addHeader(&quot;Accept&quot;, &quot;text/xml&quot;);
request.addHeader(&quot;Content-Type&quot;, &quot;text/xml;charset=utf-8&quot;);
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(&quot;Status code: &quot; + statusCode);
Assert.assertEquals(200, statusCode);
String responseString = EntityUtils.toString(response.getEntity(), &quot;UTF-8&quot;);
// Getting the Response body
System.out.println(responseString);
XmlPath jsXpath = new XmlPath(responseString);
String textResult = jsXpath.getString(&quot;respDescription&quot;);
System.out.println(&quot;result: &quot; + textResult );
}
catch (Exception ex)
{
System.out.println(&quot;Error.&quot; + ex.toString());
}
}
}

Also don't forget to add the following lines to your POM.XML in order to import all requred libraries

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

Finally the pain is over! Hope it works for you too.

huangapple
  • 本文由 发表于 2020年5月29日 06:50:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/62075813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定