简体   繁体   English

如何从 Java 类调用 SOAP Web 服务?

[英]How to do a SOAP Web Service call from Java class?

I'm relative new to the webservices world and my research seems to have confused me more than enlighten me, my problem is that I was given a library(jar) which I have to extend with some webservice functionality.我对 web 服务世界比较陌生,我的研究似乎让我感到困惑而不是启发我,我的问题是我得到了一个库(jar),我必须用一些 web 服务功能扩展它。

This library will be shared to other developers, and among the classes in the jar will be classes that have a method which calls a webservice (that essentially sets an attribute of the class, does some business logic, like storing the object in a db, etc and sends back the object with those modifications).该库将共享给其他开发人员,并且 jar 中的类将是具有调用 web 服务的方法的类(本质上设置类的属性,执行一些业务逻辑,例如将对象存储在 db 中,等,并发送回带有这些修改的对象)。 I want to make the call to this service as simple as possible, hopefully as simple so that the developer using the class only need to do.我想让对这个服务的调用尽可能简单,希望尽可能简单,以便使用该类的开发人员只需要这样做。

Car c = new Car("Blue");
c.webmethod();

I have been studying JAX-WS to use on the server but seems to me that I don't need to create a wsimport in the server nor the wsimport on the client, since I know that both have the classes, I just need some interaction between classes shared in both the server and the client.我一直在研究要在服务器上使用的 JAX-WS,但在我看来,我不需要在服务器上创建wsimport ,也不需要在客户端上创建wsimport ,因为我知道两者都有类,我只需要一些交互在服务器和客户端共享的类之间。 How do you think makes sense to do the webservice and the call in the class?你认为在课堂上做 web 服务和调用有什么意义?

I understand your problem boils down to how to call a SOAP (JAX-WS) web service from Java and get its returning object .我了解您的问题归结为如何从 Java 调用 SOAP (JAX-WS) Web 服务并获取其返回对象 In that case, you have two possible approaches:在这种情况下,您有两种可能的方法:

  1. Generate the Java classes through wsimport and use them;通过wsimport生成Java类并使用; or或者
  2. Create a SOAP client that:创建一个 SOAP 客户端:
    1. Serializes the service's parameters to XML;将服务的参数序列化为 XML;
    2. Calls the web method through HTTP manipulation;通过 HTTP 操作调用 web 方法; and
    3. Parse the returning XML response back into an object.将返回的 XML 响应解析回对象。


About the first approach (using wsimport ):关于第一种方法(使用wsimport ):

I see you already have the services' (entities or other) business classes, and it's a fact that the wsimport generates a whole new set of classes (that are somehow duplicates of the classes you already have).我看到您已经拥有服务(实体或其他)业务类,事实上wsimport生成了一组全新的类(它们在某种程度上是您已经拥有的类的重复)。

I'm afraid, though, in this scenario, you can only either:不过,恐怕在这种情况下,您只能:

  • Adapt (edit) the wsimport generated code to make it use your business classes (this is difficult and somehow not worth it - bear in mind everytime the WSDL changes, you'll have to regenerate and readapt the code);调整(编辑) wsimport生成的代码以使其使用您的业务类(这很困难,而且不知何故不值得 - 请记住,每次 WSDL 更改时,您都必须重新生成并重新调整代码); or或者
  • Give up and use the wsimport generated classes.放弃并使用wsimport生成的类。 (In this solution, you business code could "use" the generated classes as a service from another architectural layer.) (在这个解决方案中,您的业务代码可以“使用”生成的类作为来自另一个架构层的服务。)

About the second approach (create your custom SOAP client):关于第二种方法(创建您的自定义 SOAP 客户端):

In order to implement the second approach, you'll have to:为了实施第二种方法,您必须:

  1. Make the call:拨打电话:
    • Use the SAAJ (SOAP with Attachments API for Java) framework (see below, it's shipped with Java SE 1.6 or above) to make the calls;使用 SAAJ(SOAP with Attachments API for Java)框架(见下文,Java SE 1.6 或更高版本附带)进行调用; or或者
    • You can also do it through java.net.HttpUrlconnection (and some java.io handling).您也可以通过java.net.HttpUrlconnection (和一些java.io处理)来完成。
  2. Turn the objects into and back from XML:将对象转换为 XML 并从 XML 转换回来:
    • Use an OXM (Object to XML Mapping) framework such as JAXB to serialize/deserialize the XML from/into objects使用诸如 JAXB 之类的 OXM(对象到 XML 映射)框架来序列化/反序列化 XML 从/到对象
    • Or, if you must, manually create/parse the XML (this can be the best solution if the received object is only a little bit differente from the sent one).或者,如果必须,手动创建/解析 XML(如果接收到的对象与发送的对象只有一点点不同,这可能是最好的解决方案)。

Creating a SOAP client using classic java.net.HttpUrlConnection is not that hard (but not that simple either), and you can find in this link a very good starting code.使用经典的java.net.HttpUrlConnection创建 SOAP 客户端并不难(但也不是那么简单),您可以在此链接中找到一个非常好的起始代码。

I recommend you use the SAAJ framework:我推荐你使用 SAAJ 框架:

SOAP with Attachments API for Java (SAAJ) is mainly used for dealing directly with SOAP Request/Response messages which happens behind the scenes in any Web Service API. SOAP with Attachments API for Java (SAAJ)主要用于直接处理在任何 Web 服务 API 中发生在幕后的 SOAP 请求/响应消息。 It allows the developers to directly send and receive soap messages instead of using JAX-WS.它允许开发人员直接发送和接收肥皂消息,而不是使用 JAX-WS。

See below a working example (run it!) of a SOAP web service call using SAAJ.请参阅下面使用 SAAJ 的 SOAP Web 服务调用的工作示例(运行它!)。 It calls this web service .它调用这个网络服务

import javax.xml.soap.*;

public class SOAPClientSAAJ {

    // SAAJ - SOAP Client Testing
    public static void main(String args[]) {
        /*
            The example below requests from the Web Service at:
             https://www.w3schools.com/xml/tempconvert.asmx?op=CelsiusToFahrenheit


            To call other WS, change the parameters below, which are:
             - the SOAP Endpoint URL (that is, where the service is responding from)
             - the SOAP Action

            Also change the contents of the method createSoapEnvelope() in this class. It constructs
             the inner part of the SOAP envelope that is actually sent.
         */
        String soapEndpointUrl = "https://www.w3schools.com/xml/tempconvert.asmx";
        String soapAction = "https://www.w3schools.com/xml/CelsiusToFahrenheit";

        callSoapWebService(soapEndpointUrl, soapAction);
    }

    private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String myNamespace = "myNamespace";
        String myNamespaceURI = "https://www.w3schools.com/xml/";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);

            /*
            Constructed SOAP Request Message:
            <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:myNamespace="https://www.w3schools.com/xml/">
                <SOAP-ENV:Header/>
                <SOAP-ENV:Body>
                    <myNamespace:CelsiusToFahrenheit>
                        <myNamespace:Celsius>100</myNamespace:Celsius>
                    </myNamespace:CelsiusToFahrenheit>
                </SOAP-ENV:Body>
            </SOAP-ENV:Envelope>
            */

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("CelsiusToFahrenheit", myNamespace);
        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("Celsius", myNamespace);
        soapBodyElem1.addTextNode("100");
    }

    private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
        try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);

            // Print the SOAP Response
            System.out.println("Response SOAP Message:");
            soapResponse.writeTo(System.out);
            System.out.println();

            soapConnection.close();
        } catch (Exception e) {
            System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
            e.printStackTrace();
        }
    }

    private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        createSoapEnvelope(soapMessage);

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", soapAction);

        soapMessage.saveChanges();

        /* Print the request message, just for debugging purposes */
        System.out.println("Request SOAP Message:");
        soapMessage.writeTo(System.out);
        System.out.println("\n");

        return soapMessage;
    }

}

About using JAXB for serializing/deserializing, it is very easy to find information about it.关于使用 JAXB 进行序列化/反序列化,很容易找到有关它的信息。 You can start here: http://www.mkyong.com/java/jaxb-hello-world-example/ .你可以从这里开始: http ://www.mkyong.com/java/jaxb-hello-world-example/。

Or just use Apache CXF's wsdl2java to generate objects you can use.或者只是使用Apache CXF 的 wsdl2java来生成您可以使用的对象。

It is included in the binary package you can download from their website.它包含在二进制包中,您可以从他们的网站下载。 You can simply run a command like this:您可以简单地运行如下命令:

$ ./wsdl2java -p com.mynamespace.for.the.api.objects -autoNameResolution http://www.someurl.com/DefaultWebService?wsdl

It uses the wsdl to generate objects, which you can use like this (object names are also grabbed from the wsdl, so yours will be different a little):它使用 wsdl 生成对象,您可以像这样使用它(对象名称也从 wsdl 中获取,因此您的名称会有所不同):

DefaultWebService defaultWebService = new DefaultWebService();
String res = defaultWebService.getDefaultWebServiceHttpSoap11Endpoint().login("webservice","dadsadasdasd");
System.out.println(res);

There is even a Maven plug-in which generates the sources: https://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html甚至还有一个 Maven 插件可以生成源代码: https ://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html

Note: If you generate sources using CXF and IDEA, you might want to look at this: https://stackoverflow.com/a/46812593/840315注意:如果您使用 CXF 和 IDEA 生成源代码,您可能需要查看: https ://stackoverflow.com/a/46812593/840315

Might help for someone who have xml request as string.可能对将 xml 请求作为字符串的人有所帮助。 if you have WSDL, You can create a new soap request in SoapUI with that WSDL file.如果您有 WSDL,您可以使用该 WSDL 文件在 SoapUI 中创建一个新的肥皂请求。
It would automatically generate the Structure/XML for input request.它将自动为输入请求生成结构/XML。

Here is some simple version of Java code you can use to call Soap service if you have the input request xml from SoapUI:如果您有来自 SoapUI 的输入请求 xml,则可以使用以下 Java 代码的一些简单版本来调用 Soap 服务:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class SimpleSoapClient {

public static void main(String args[]) throws IOException {
        
    String address="Hyderabad";

    /* place your xml request from soap ui below with necessary changes in parameters*/
    
    String xml="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://www.YourUrlAsPerWsdl.com/\">\r\n" + 
                 "   <soapenv:Header/>\r\n" + 
                 "   <soapenv:Body>\r\n" + 
                 "      <ws:callRest>\r\n" + 
                 "         <name>"+"Hello"+"</name>\r\n" + 
                 "         <address>"+address+"</address>\r\n" + 
                 "      </ws:callRest>\r\n" + 
                 "   </soapenv:Body>\r\n" + 
                 "</soapenv:Envelope>";
            String responseF=callSoapService(xml);
            System.out.println(responseF);
    }
    

}

static String callSoapService(String soapRequest) {
    try {
     String url = "https://gogle.com/service/hello"; // replace your URL here
     URL obj = new URL(url);
     HttpURLConnection con = (HttpURLConnection) obj.openConnection();
     
     // change these values as per soapui request on top left of request, click on RAW, you will find all the headers
     con.setRequestMethod("POST");
     con.setRequestProperty("Content-Type","text/xml; charset=utf-8"); 
     con.setDoOutput(true);
     DataOutputStream wr = new DataOutputStream(con.getOutputStream());
     wr.writeBytes(soapRequest);
     wr.flush();
     wr.close();
     String responseStatus = con.getResponseMessage();
     System.out.println(responseStatus);
     BufferedReader in = new BufferedReader(new InputStreamReader(
     con.getInputStream()));
     String inputLine;
     StringBuffer response = new StringBuffer();
     while ((inputLine = in.readLine()) != null) {
         response.append(inputLine);
     }
     in.close();
     
     // You can play with response which is available as string now:
     String finalvalue= response.toString();
     
     // or you can parse/substring the required tag from response as below based your response code
     finalvalue= finalvalue.substring(finalvalue.indexOf("<response>")+10,finalvalue.indexOf("</response>")); */
     
     return finalvalue;
     } 
    catch (Exception e) {
        return e.getMessage();
    }   
}

} }

I found a much simpler alternative way to generating soap message.我找到了一种更简单的生成肥皂消息的替代方法。 Given a Person Object:给定一个 Person 对象:

import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Person {
  private String name;
  private int age;
  private String address; //setter and getters below
}

Below is a simple Soap Message Generator:下面是一个简单的 Soap 消息生成器:

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

@Slf4j
public class SoapGenerator {

  protected static final ObjectMapper XML_MAPPER = new XmlMapper()
      .enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)
      .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
      .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
      .registerModule(new JavaTimeModule());

  private static final String SOAP_BODY_OPEN = "<soap:Body>";
  private static final String SOAP_BODY_CLOSE = "</soap:Body>";
  private static final String SOAP_ENVELOPE_OPEN = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
  private static final String SOAP_ENVELOPE_CLOSE = "</soap:Envelope>";

  public static String soapWrap(String xml) {
    return SOAP_ENVELOPE_OPEN + SOAP_BODY_OPEN + xml + SOAP_BODY_CLOSE + SOAP_ENVELOPE_CLOSE;
  }

  public static String soapUnwrap(String xml) {
    return StringUtils.substringBetween(xml, SOAP_BODY_OPEN, SOAP_BODY_CLOSE);
  }
}

You can use by:您可以通过以下方式使用:

 public static void main(String[] args) throws Exception{
        Person p = new Person();
        p.setName("Test");
        p.setAge(12);

        String xml = SoapGenerator.soapWrap(XML_MAPPER.writeValueAsString(p));
        log.info("Generated String");
        log.info(xml);
      }

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

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