从第三方SOAP服务访问XML?

时间:2011-10-01 23:41:36

标签: soap jersey jax-rs

我正在使用供应商创建的SOAP客户端在我的Jersey 1.3 REST应用程序中访问他们的SOAP服务。

在某些情况下,我想访问响应的XML,而不是客户端的代理类。有没有办法做到这一点?

如果能让这更容易,我也可以访问他们的WSDL。

1 个答案:

答案 0 :(得分:0)

您可以使用JAX-WS Dispatch客户端来实现XML:

import java.io.FileInputStream;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

import org.w3c.dom.Node;

public class DispatchClient {
    public static void main(String[] args) throws Exception {

        String wsdlAddress = "http://127.0.0.1:8080/news/NewsWS?wsdl";
        URL wsdl = new URL(wsdlAddress);

        QName serviceName = new QName("http://news/", "NewsWebService");
        QName portName = new QName("http://news/", "NewsPort");

        //nie ma WSDL-a
        Service service = Service.create(serviceName);
        service.addPort(portName, SOAPBinding.SOAP12HTTP_BINDING, "http://127.0.0.1:8080/news/NewsWS");     


        Dispatch<SOAPMessage> dispatch = service.createDispatch(portName,
                SOAPMessage.class, Service.Mode.MESSAGE);

        MessageFactory mf = MessageFactory.newInstance();
        SOAPMessage req = mf.createMessage(null, new FileInputStream("NewsCountSoapRequest.xml"));

        SOAPMessage res = dispatch.invoke(req);

        SOAPBody body = res.getSOAPBody(); //SOAP body XML
    }
}

您可以使用DOM接口使用SOAP body XML(所有这些Node疯狂)或使用XPath。