简体   繁体   English

如何从Java访问SOAP Web服务URL端点

[英]How to access a SOAP Web Service URL Endpoint from Java

How to access a SOAP Web Service URL Endpoint from Java? 如何从Java访问SOAP Web服务URL端点?

I've been searching for a concrete example for a while and I'm getting no-where. 我一直在寻找一个具体的例子有一段时间了,但是却无处可寻。 Unfortunately my preferred method using REST isn't available in this scenario I'm working on, so having to test a SOAP based approach. 不幸的是,我在使用的这种情况下无法使用我首选的REST方法,因此必须测试基于SOAP的方法。

The setup is, there is a SOAP WSDL file located at www.website.com/soap.wsdl for example. 设置是,例如,在www.website.com/soap.wsdl有一个SOAP WSDL文件。 And I'm trying to send and receive data from a CreateData endpoint. 我正在尝试从CreateData端点发送和接收数据。

When I add the WSDL file into SoapUI software to test, this generates the following SOAP message template; 当我将WSDL文件添加到SoapUI软件进行测试时,这将生成以下SOAP消息模板;

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tic="{redacted}">
   <soapenv:Header/>
   <soapenv:Body>
      <tic:CreateData>
         <tic:request>{"testKey":"testValue"}</tic:request>
      </tic:CreateData>
   </soapenv:Body>
</soapenv:Envelope>

(I added the test JSON data) (我添加了测试JSON数据)

What I can't seem to figure out is how to turn this request which works correctly in SoapUI into a functioning piece of test Java code. 我似乎无法弄清楚的是如何将该在SoapUI中正常工作的请求转换为功能正常的测试Java代码。

When I import the WSDL into my IDE, it generates a single Java file which looks like this; 当我将WSDL导入我的IDE时,它将生成一个看起来像这样的Java文件。

import javax.jws.WebService;

@WebService(serviceName = "{redacted}", portName = "{redacted}", endpointInterface = "{redacted}", targetNamespace = "{redacted}", wsdlLocation = "WEB-INF/wsdl/null/null.wsdl")
public class NewWebServiceFromWSDL {

    public java.lang.String createData(java.lang.String request) {
        //TODO implement this method
        throw new UnsupportedOperationException("Not implemented yet.");
    }

}

Naturally, I now have no idea what to do next. 自然,我现在不知道下一步该怎么做。 How do I go about creating a SOAP Envelope in Java? 如何使用Java创建SOAP信封? I would have expected to see some methods generated for things such as setSOAPEnvelopeHeader(), setSOAPEnvelopeBody(), setSOAPEnvelopeVariableRequest() etc. (or similar). 我本来希望看到为诸如setSOAPEnvelopeHeader(),setSOAPEnvelopeBody(),setSOAPEnvelopeVariableRequest()等(或类似)之类的东西生成的一些方法。

Then once the SOAP Envelope object has been created (however that is done), how do I then go about actually sending the message and handling the response? 然后,一旦创建了SOAP Envelope对象(无论如何完成),那么我该如何实际发送消息并处理响应呢?

Feeling confused... 感觉困惑...

This is cruel way of calling SOAP, but it works, and will be good to get going. 这是调用SOAP的残酷方式,但是它可以工作,并且很容易上手。 Then, you do some modifications to deal with SOAPEnvelope/SOAPBody and that's all. 然后,您需要进行一些修改以处理SOAPEnvelope / SOAPBody,仅此而已。

package xml;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;

import com.sun.xml.internal.ws.encoding.soap.SOAP12Constants;


public class SOAPCall {

public static void main(String[] args) throws UnsupportedOperationException, SOAPException, IOException {

    String endPoint = "URL";

    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection soapConnection = soapConnectionFactory.createConnection();

    String soapString = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tic=\"{redacted}\">"
            + "<soapenv:Header/><soapenv:Body>"
            + "<tic:CreateData><tic:request>{\"testKey\":\"testValue\"}</tic:request>"
            + "</tic:CreateData></soapenv:Body></soapenv:Envelope>";

    InputStream is = new ByteArrayInputStream(soapString.getBytes());

    SOAPMessage request = MessageFactory.newInstance().createMessage(null, is);

    MimeHeaders headers = request.getMimeHeaders();
    // If SOAP Header is compulsory
    // headers.addHeader("SOAPAction",
    // "http://www.tdcare.com/DataInterchange");
    headers.setHeader("Content-Type", "application/xml");
    request.saveChanges();

    SOAPMessage soapResponse = soapConnection.call(request, endPoint);

    System.out.println(soapResponse);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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