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

时间:2013-04-11 03:28:42

标签: java web-services soap jax-ws saaj

我对网络服务世界相对较新,我的研究似乎让我困惑不仅仅是启发我,我的问题是我得到了一个库(jar),我必须扩展一些web服务功能。

这个库将与其他开发人员共享,并且jar中的类将是具有调用webservice的方法的类(实质上设置类的属性,执行一些业务逻辑,如将对象存储在一个db等,并发回带有这些修改的对象)。我想尽可能简单地调用这个服务,希望这很简单,这样开发人员只需要使用该类。

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

我一直在研究在服务器上使用的JAX-WS,但在我看来,我不需要在服务器上创建wsimport,也不需要在客户端上创建wsimport,因为我知道两者都有类,我只需要在服务器和客户端共享的类之间进行一些交互。您认为在课堂上进行网络服务和通话是否有意义?

4 个答案:

答案 0 :(得分:254)

我理解你的问题归结为如何从Java调用SOAP(JAX-WS)Web服务并获取其返回对象。在这种情况下,您有两种可能的方法:

  1. 通过wsimport生成Java类并使用它们;或
  2. 创建一个SOAP客户端:
    1. 将服务的参数序列化为XML;
    2. 通过HTTP操作调用Web方法;和
    3. 将返回的XML响应解析回对象。

  3. 关于第一种方法(使用wsimport):

    我看到你已经有了服务'(实体或其他)业务类,而且wsimport生成了一组全新的类(这些类与你已经拥有的类重复)。

    但是,我担心,在这种情况下,你只能:

    • 调整(编辑)wsimport生成的代码,使其使用您的业务类(这很困难,不知何故不值得) - 每次WSDL更改时都要记住,你会必须重新生成并重新编写代码);或
    • 放弃并使用wsimport生成的类。 (在此解决方案中,业务代码可以将生成的类“用作”来自另一个体系结构层的服务。)

    关于第二种方法(创建自定义SOAP客户端):

    为了实施第二种方法,您必须:

    1. 拨打电话:
      • 使用SAAJ(SOAP with Attachments API for Java)框架(见下文,Java SE 1.6或更高版本附带)进行调用;或
      • 您也可以通过java.net.HttpUrlconnection(和一些java.io处理)来完成。
    2. 将对象从XML转入和转回:
      • 使用OXM(对象到XML映射)框架(如JAXB)从/向对象序列化/反序列化XML
      • 或者,如果必须,手动创建/解析XML(如果收到的对象与发送的对象有点不同,这可能是最好的解决方案。)
    3. 使用经典java.net.HttpUrlConnection创建SOAP客户端并不是那么难(但也不是那么简单),您可以在this link中找到一个非常好的起始代码。

      我建议你使用SAAJ框架:

        

      SOAP with Attachments API for Java(SAAJ)主要用于直接处理在任何Web Service API的幕后发生的SOAP请求/响应消息。它允许开发人员直接发送和接收soap消息,而不是使用JAX-WS。

      使用SAAJ查看SOAP Web服务调用的工作示例(运行它!)。它调用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;
          }
      
      }
      

      关于使用JAXB进行序列化/反序列化,很容易找到有关它的信息。你可以从这里开始:http://www.mkyong.com/java/jaxb-hello-world-example/

答案 1 :(得分:3)

或者只使用Apache CXF's wsdl2java生成可以使用的对象。

它包含在您可以从他们的网站下载的二进制包中。您只需运行如下命令:

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

它使用wsdl生成对象,您可以像这样使用对象名称(也可以从wsdl中获取对象名称,因此您的对象会有所不同):

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

甚至还有一个Maven插件可以生成源:https://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html

注意:如果您使用CXF和IDEA生成源代码,您可能需要查看以下内容:https://stackoverflow.com/a/46812593/840315

答案 2 :(得分:0)

可能对将 xml 请求作为字符串的人有所帮助。 如果您有 WSDL,您可以使用该 WSDL 文件在 SoapUI 中创建一个新的soap请求。
它会自动为输入请求生成 Structure/XML。

如果您有来自 SoapUI 的输入请求 xml,这里是一些可用于调用 Soap 服务的简单 Java 代码版本:

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();
    }   
}

}

答案 3 :(得分:-1)

我找到了一种生成肥皂消息的简单得多的替代方法。 给定一个人物对象:

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
}

下面是一个简单的Soap Message Generator:

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);
  }
}

您可以使用:

 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);
      }