简体   繁体   中英

How to do a call through a javax.xml.ws.Service

Created a new standard java 7 project in Eclipse and have successfully managed to get an instance of a javax.xml.ws.Service like so:

  String wsdlURL = "http://example.com:3000/v1_0/foo/bar/SomeService?wsdl";
  String namespace = "http://foo.bar.com/webservice";
  String serviceName = "SomeService";
  QName serviceQN = new QName(namespace, serviceName);

  Service service = Service.create(new URL(wsdlURL), serviceQN);

This runs fine in a main method, so as far as I can see, that part works. But I can't figure out how to actually use it. In SoapUI I call this same service with a request which looks like the following:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://foo.bar.com/webservice">
   <soapenv:Header/>
   <soapenv:Body>
      <web:SomeWebServiceRequest acAccountName="name" acAccountPassword="password">
         <SomeRequest>
            <id>012345678901234</id>
            <action>Fix</action>
         </SomeRequest>
      </web:SomeWebServiceRequest>
   </soapenv:Body>
</soapenv:Envelope>

How can I do the same request in Java? My goal is that I have a long list of these id s, and I need to run a request like that for each of them. Doing it manually in SoapUI is a bit annoying, so I'd like to automate it with a simple Java console application.

Next step is to get Port from your service:

Service service = Service.create(new URL(wsdlURL), serviceQN); // this is where you are.
QName portQName = new QName(portNamespace, portName);
YourPortInterface port = service.getPort(portQName, YourPortInterface.class);

YourPortInteface will be generated during wsimport or you can create and annotate it by yourself if you have enough experience in "reading" wsdl.

You can use JAX-WS as a client.

Basically you use wsimport to create stub java classes which wrap the web service, then you use those stubs in your java code. The stubs take care of all the XML translation, both for your request and response.

The tutorial is here: http://docs.oracle.com/javaee/5/tutorial/doc/bnayn.html
Look for the part called: "A Simple JAX-WS Client"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM