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

别来无恙 提交于 2019-12-09 00:57:15

问题


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 ids, 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.


回答1:


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.




回答2:


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"



来源:https://stackoverflow.com/questions/9922655/how-to-do-a-call-through-a-javax-xml-ws-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!