Java WS-JUnit测试:找不到{}的分派方法

Java WS-JUnit测试:找不到{}的分派方法,java,web-services,soap,junit,Java,Web Services,Soap,Junit,我必须用Java开发几个WS。因为我以前没有这方面的经验,我从一个简单的例子开始。这里是WS: @WebService public interface DataExchangeWebService { public int doubleIt(int numberToDouble); } @WebService(targetNamespace = "http://www.example.com/ws/DataExchange", portName = "Da

我必须用Java开发几个WS。因为我以前没有这方面的经验,我从一个简单的例子开始。这里是WS:

@WebService
public interface DataExchangeWebService {

    public int doubleIt(int numberToDouble);

}

@WebService(targetNamespace = "http://www.example.com/ws/DataExchange",
            portName = "DataExchangePort",
            serviceName = "DataExchangeService",
            endpointInterface = "xyz.DataExchangeWebService")
public class DataExchangeWebServiceImpl implements DataExchangeWebService {

    @Override
    public int doubleIt(int numberToDouble) {
        return numberToDouble * 2;
    }

}
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is 
    JAX-WS RI 2.1.6 in JDK 6. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is 
    JAX-WS RI 2.1.6 in JDK 6. -->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:tns="http://www.example.com/ws/DataExchange" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://www.example.com/ws/DataExchange"
    name="DataExchangeService">
    <import namespace="http://dataexchange.sys.cs.iface.service.appserver.xyz/"
        location="http://localhost:9000/ws/DataExchange?wsdl=1" />
    <binding xmlns:ns1="http://dataexchange.sys.cs.iface.service.appserver.xyz/"
        name="DataExchangePortBinding" type="ns1:DataExchangeWebService">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http"
            style="document" />
        <operation name="doubleIt">
            <soap:operation soapAction="" />
            <input>
                <soap:body use="literal" />
            </input>
            <output>
                <soap:body use="literal" />
            </output>
        </operation>
    </binding>
    <service name="DataExchangeService">
        <port name="DataExchangePort" binding="tns:DataExchangePortBinding">
            <soap:address location="http://localhost:9000/ws/DataExchange" />
        </port>
    </service>
</definitions>
public void raw_service() {
    Service service = Service.create(wsdlURL, serviceName);
    DataExchangeWebService dataExchangeWebService = service.getPort(portName, DataExchangeWebService.class);
    int resp = dataExchangeWebService.doubleIt(10);
    assertEquals(20, resp);
}
 public void raw_service_with_full_soap_message() throws DOMException, SOAPException, IOException {
        Service service = Service.create(wsdlURL, serviceName);
        Dispatch<SOAPMessage> disp = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
        InputStream is = getClass().getClassLoader().getResourceAsStream("fullSOAPMessage.xml");
        SOAPMessage reqMsg = MessageFactory.newInstance().createMessage(null, is);
        assertNotNull(reqMsg);
        SOAPMessage response = disp.invoke(reqMsg);
        assertEquals("Double-It not doubling zero correctly", "0", response.getSOAPBody().getTextContent().trim());
    }
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:doubleIt xmlns:ns2="http://www.example.com/ws/DataExchange">
         <numberToDouble>0</numberToDouble>
      </ns2:doubleIt>
   </soap:Body>
</soap:Envelope>
javax.xml.ws.soap.SOAPFaultException: Cannot find dispatch method for {http://www.example.com/ws/DataExchange}doubleIt
现在我只想通过JUnit测试这项服务。我找到了博客条目,它为我的测试用例构建了起点。为了保持简单,我使用了Java端点类。

protected static URL wsdlURL;

protected static QName serviceName;

protected static QName portName;

protected static Endpoint endpoint;

protected static String address;

static {
    serviceName = new QName("http://www.example.com/ws/DataExchange", "DataExchangeService");
    portName = new QName("http://www.example.com/ws/DataExchange", "DataExchangePort");
}

@BeforeClass
public static void setUp() throws MalformedURLException {
    address = "http://localhost:9000/ws/DataExchange";
    wsdlURL = new URL(address + "?wsdl");
    endpoint = Endpoint.publish(address, new DataExchangeWebServiceImpl());
}

@AfterClass
public static void tearDown() {
    endpoint.stop();
}
这将发布WS并生成以下WSDL:

@WebService
public interface DataExchangeWebService {

    public int doubleIt(int numberToDouble);

}

@WebService(targetNamespace = "http://www.example.com/ws/DataExchange",
            portName = "DataExchangePort",
            serviceName = "DataExchangeService",
            endpointInterface = "xyz.DataExchangeWebService")
public class DataExchangeWebServiceImpl implements DataExchangeWebService {

    @Override
    public int doubleIt(int numberToDouble) {
        return numberToDouble * 2;
    }

}
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is 
    JAX-WS RI 2.1.6 in JDK 6. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is 
    JAX-WS RI 2.1.6 in JDK 6. -->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:tns="http://www.example.com/ws/DataExchange" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://www.example.com/ws/DataExchange"
    name="DataExchangeService">
    <import namespace="http://dataexchange.sys.cs.iface.service.appserver.xyz/"
        location="http://localhost:9000/ws/DataExchange?wsdl=1" />
    <binding xmlns:ns1="http://dataexchange.sys.cs.iface.service.appserver.xyz/"
        name="DataExchangePortBinding" type="ns1:DataExchangeWebService">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http"
            style="document" />
        <operation name="doubleIt">
            <soap:operation soapAction="" />
            <input>
                <soap:body use="literal" />
            </input>
            <output>
                <soap:body use="literal" />
            </output>
        </operation>
    </binding>
    <service name="DataExchangeService">
        <port name="DataExchangePort" binding="tns:DataExchangePortBinding">
            <soap:address location="http://localhost:9000/ws/DataExchange" />
        </port>
    </service>
</definitions>
public void raw_service() {
    Service service = Service.create(wsdlURL, serviceName);
    DataExchangeWebService dataExchangeWebService = service.getPort(portName, DataExchangeWebService.class);
    int resp = dataExchangeWebService.doubleIt(10);
    assertEquals(20, resp);
}
 public void raw_service_with_full_soap_message() throws DOMException, SOAPException, IOException {
        Service service = Service.create(wsdlURL, serviceName);
        Dispatch<SOAPMessage> disp = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
        InputStream is = getClass().getClassLoader().getResourceAsStream("fullSOAPMessage.xml");
        SOAPMessage reqMsg = MessageFactory.newInstance().createMessage(null, is);
        assertNotNull(reqMsg);
        SOAPMessage response = disp.invoke(reqMsg);
        assertEquals("Double-It not doubling zero correctly", "0", response.getSOAPBody().getTextContent().trim());
    }
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:doubleIt xmlns:ns2="http://www.example.com/ws/DataExchange">
         <numberToDouble>0</numberToDouble>
      </ns2:doubleIt>
   </soap:Body>
</soap:Envelope>
javax.xml.ws.soap.SOAPFaultException: Cannot find dispatch method for {http://www.example.com/ws/DataExchange}doubleIt
这个很好用。问题是第二个。它使用SOAP消息发出请求:

@WebService
public interface DataExchangeWebService {

    public int doubleIt(int numberToDouble);

}

@WebService(targetNamespace = "http://www.example.com/ws/DataExchange",
            portName = "DataExchangePort",
            serviceName = "DataExchangeService",
            endpointInterface = "xyz.DataExchangeWebService")
public class DataExchangeWebServiceImpl implements DataExchangeWebService {

    @Override
    public int doubleIt(int numberToDouble) {
        return numberToDouble * 2;
    }

}
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is 
    JAX-WS RI 2.1.6 in JDK 6. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is 
    JAX-WS RI 2.1.6 in JDK 6. -->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:tns="http://www.example.com/ws/DataExchange" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://www.example.com/ws/DataExchange"
    name="DataExchangeService">
    <import namespace="http://dataexchange.sys.cs.iface.service.appserver.xyz/"
        location="http://localhost:9000/ws/DataExchange?wsdl=1" />
    <binding xmlns:ns1="http://dataexchange.sys.cs.iface.service.appserver.xyz/"
        name="DataExchangePortBinding" type="ns1:DataExchangeWebService">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http"
            style="document" />
        <operation name="doubleIt">
            <soap:operation soapAction="" />
            <input>
                <soap:body use="literal" />
            </input>
            <output>
                <soap:body use="literal" />
            </output>
        </operation>
    </binding>
    <service name="DataExchangeService">
        <port name="DataExchangePort" binding="tns:DataExchangePortBinding">
            <soap:address location="http://localhost:9000/ws/DataExchange" />
        </port>
    </service>
</definitions>
public void raw_service() {
    Service service = Service.create(wsdlURL, serviceName);
    DataExchangeWebService dataExchangeWebService = service.getPort(portName, DataExchangeWebService.class);
    int resp = dataExchangeWebService.doubleIt(10);
    assertEquals(20, resp);
}
 public void raw_service_with_full_soap_message() throws DOMException, SOAPException, IOException {
        Service service = Service.create(wsdlURL, serviceName);
        Dispatch<SOAPMessage> disp = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
        InputStream is = getClass().getClassLoader().getResourceAsStream("fullSOAPMessage.xml");
        SOAPMessage reqMsg = MessageFactory.newInstance().createMessage(null, is);
        assertNotNull(reqMsg);
        SOAPMessage response = disp.invoke(reqMsg);
        assertEquals("Double-It not doubling zero correctly", "0", response.getSOAPBody().getTextContent().trim());
    }
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:doubleIt xmlns:ns2="http://www.example.com/ws/DataExchange">
         <numberToDouble>0</numberToDouble>
      </ns2:doubleIt>
   </soap:Body>
</soap:Envelope>
javax.xml.ws.soap.SOAPFaultException: Cannot find dispatch method for {http://www.example.com/ws/DataExchange}doubleIt
我到目前为止所做的研究只揭示了这与WS的目标命名空间和SOAP消息之间的冲突有关。但在我看来,情况并非如此。 关于如何解决这个问题或发生了什么的一些想法?