IntelliJ Web Client Errors

2020-07-29 17:34发布

问题:

I'm trying to create a Java web service client with IntelliJ IDEA, but am getting errors. My web service "MagicEightBall" is a minimalistic service that has one method: "GetAdvice", which takes no arguments and returns a string. When I use IntelliJ to create a WebServices client (using GlashFish / JAXWS 2.X RI / Metro 1.X / JWSDP 2.0 ), I get the following code:

public class HelloWorldClient {
  public static void main(String[] argv) {
      pakeightball.IMagicEightBallService service = new MagicEightBallService().();
      //invoke business method
      service.getAdvice();  
  }
}

I tried this with a WCF basicHttpBinding service, fixed the code a little, and it worked fine. I'm now trying to connect to a WCF wsHttpBinding (uses SSL) service, but am getting an error. First, here's the fixed up code (I had to disable certificate validation):

import javax.net.ssl.*;
import pakeightball.*;

public class HelloWorldClient {

    public static void DisableCertificateValidation() {
          TrustManager[] trustAllCerts = new TrustManager[]{
              new X509TrustManager() {
                  public X509Certificate[] getAcceptedIssuers() { return null; }
                  public void checkClientTrusted(X509Certificate[] certs, String authType) { }
                  public void checkServerTrusted(X509Certificate[] certs, String authType) { }
              }
          };
          try {
              SSLContext sc = SSLContext.getInstance("SSL");
              sc.init(null, trustAllCerts, new java.security.SecureRandom());
              HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
          } catch (Exception e) {
          }
    }

    public static void main(String[] argv) {
      MagicEightBallService locator = new MagicEightBallService();
      IMagicEightBallService service = locator.getWSHttpBindingTransportSecurityIMagicEightBallService();

      // disable certificate validation
      DisableCertificateValidation();

      //invoke business method
      String result = service.getAdvice();
      System.out.println(result);
    }

}

This code is compiling but gives the following runtime error:

"C:\Program Files\Java\jdk1.6.0_26\bin\java" -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 10.5.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.6.0_26\jre\lib\alt-rt.jar;C:\Program Files\Java\jdk1.6.0_26\jre\lib\alt-string.jar;C:\Program Files\Java\jdk1.6.0_26\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.6.0_26\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.6.0_26\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.6.0_26\jre\lib\jce.jar;C:\Program Files\Java\jdk1.6.0_26\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0_26\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.6.0_26\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.6.0_26\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0_26\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_26\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.6.0_26\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.6.0_26\jre\lib\ext\sunjce_provider.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\out\production\TryMagicEightBallClient3;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\activation.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\FastInfoset.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\http.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\jaxb-api.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\jaxb-impl.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\jaxb-xjc.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\jaxws-api.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\jaxws-rt.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\jaxws-tools.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\jsr173_api.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\jsr181-api.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\jsr250-api.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\resolver.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\saaj-api.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\saaj-impl.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\sjsxp.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\stax-ex.jar;C:\Users\Jay.MVP\IdeaProjects\TryMagicEightBallClient3\lib\streambuffer.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 10.5.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain example.HelloWorldClient
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: MustUnderstand headers:[{http://www.w3.org/2005/08/addressing}Action] are not understood
    at com.sun.xml.ws.protocol.soap.MUTube.createMUSOAPFaultException(MUTube.java:140)
    at com.sun.xml.ws.protocol.soap.ClientMUTube.processResponse(ClientMUTube.java:82)
    at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:569)
    at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:518)
    at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:503)
    at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:400)
    at com.sun.xml.ws.client.Stub.process(Stub.java:228)
    at com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStub.java:120)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:226)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:206)
    at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:103)
    at $Proxy30.getAdvice(Unknown Source)
    at example.HelloWorldClient.main(HelloWorldClient.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Process finished with exit code 1

Is IntelliJ IDEA's WebService client able to consume WCF services other than basicHttpBinding? Am I doing something wrong here? Any guidance would be appreciated.

Thanks!

EDIT:

Here's the Web.config for the WCF service:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

  <system.serviceModel>
    <services>
      <service name="Petio.MagicEightBall.MagicEightBallService" behaviorConfiguration="MagicEightBallServiceBehavior">

        <endpoint name="WSHttpBinding_TransportSecurity_IMagicEightBallService"
                  address="https://localhost/MagicEightBall/MagicEightBallService.svc"
                  binding="wsHttpBinding"
                  bindingConfiguration="TransportSecurity"
                  contract="Petio.MagicEightBall.IMagicEightBallService" />
        <endpoint address="mex"
                  binding="mexHttpsBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="MagicEightBallServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <bindings>
      <wsHttpBinding>
        <binding name="TransportSecurity">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </wsHttpBinding>

    </bindings>


  </system.serviceModel>
</configuration>

Here's the WSDL that's generated:

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:tns="http://tempuri.org/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" name="MagicEightBallService" targetNamespace="http://tempuri.org/">
  <wsp:Policy wsu:Id="WSHttpBinding_TransportSecurity_IMagicEightBallService_policy">
    <wsp:ExactlyOne>
      <wsp:All>
        <sp:TransportBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
          <wsp:Policy>
            <sp:TransportToken>
              <wsp:Policy>
                <sp:HttpsToken RequireClientCertificate="false"/>
              </wsp:Policy>
            </sp:TransportToken>
            <sp:AlgorithmSuite>
              <wsp:Policy>
                <sp:Basic256/>
              </wsp:Policy>
            </sp:AlgorithmSuite>
            <sp:Layout>
              <wsp:Policy>
                <sp:Strict/>
              </wsp:Policy>
            </sp:Layout>
          </wsp:Policy>
        </sp:TransportBinding>
        <wsaw:UsingAddressing/>
      </wsp:All>
    </wsp:ExactlyOne>
  </wsp:Policy>
  <wsdl:types>
    <xsd:schema targetNamespace="http://tempuri.org/Imports">
      <xsd:import schemaLocation="http://my.local.domain.name/MagicEightBall/MagicEightBallService.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
      <xsd:import schemaLocation="http://my.local.domain.name/MagicEightBall/MagicEightBallService.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="IMagicEightBallService_GetAdvice_InputMessage">
    <wsdl:part name="parameters" element="tns:GetAdvice"/>
  </wsdl:message>
  <wsdl:message name="IMagicEightBallService_GetAdvice_OutputMessage">
    <wsdl:part name="parameters" element="tns:GetAdviceResponse"/>
  </wsdl:message>
  <wsdl:portType name="IMagicEightBallService">
    <wsdl:operation name="GetAdvice">
      <wsdl:input wsaw:Action="http://tempuri.org/IMagicEightBallService/GetAdvice" message="tns:IMagicEightBallService_GetAdvice_InputMessage"/>
      <wsdl:output wsaw:Action="http://tempuri.org/IMagicEightBallService/GetAdviceResponse" message="tns:IMagicEightBallService_GetAdvice_OutputMessage"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="WSHttpBinding_TransportSecurity_IMagicEightBallService" type="tns:IMagicEightBallService">
    <wsp:PolicyReference URI="#WSHttpBinding_TransportSecurity_IMagicEightBallService_policy"/>
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="GetAdvice">
      <soap12:operation soapAction="http://tempuri.org/IMagicEightBallService/GetAdvice" style="document"/>
      <wsdl:input>
        <soap12:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="MagicEightBallService">
    <wsdl:port name="WSHttpBinding_TransportSecurity_IMagicEightBallService" binding="tns:WSHttpBinding_TransportSecurity_IMagicEightBallService">
      <soap12:address location="https://localhost/MagicEightBall/MagicEightBallService.svc"/>
      <wsa10:EndpointReference>
        <wsa10:Address>
          https://localhost/MagicEightBall/MagicEightBallService.svc
        </wsa10:Address>
      </wsa10:EndpointReference>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

回答1:

I would change the following line...

IMagicEightBallService service = locator.getWSHttpBindingTransportSecurityIMagicEightBallService();

for...

IMagicEightBallService service = locator.getWSHttpBindingTransportSecurityIMagicEightBallService(new javax.xml.ws.soap.AddressingFeature());

It worked for me.