116

我对 web 服务世界比较陌生,我的研究似乎让我感到困惑而不是启发我,我的问题是我得到了一个库(jar),我必须用一些 web 服务功能扩展它。

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

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

我一直在研究 JAX-WS 以在服务器上使用,但在我看来,我不需要wsimport在服务器或wsimport客户端上创建一个,因为我知道两者都有类,我只需要类之间的一些交互在服务器和客户端共享。你认为在课堂上做 web 服务和调用有什么意义?

4

4 回答 4

279

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

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


关于第一种方法(使用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 并从 XML 转换回来:
    • 使用诸如 JAXB 之类的 OXM(对象到 XML 映射)框架来序列化/反序列化 XML 从/到对象
    • 或者,如果必须,手动创建/解析 XML(如果接收到的对象与发送的对象只有一点点不同,这可能是最好的解决方案)。

使用经典创建 SOAP 客户端java.net.HttpUrlConnection并不难(但也不是那么简单),您可以在此链接中找到非常好的起始代码。

我推荐你使用 SAAJ 框架:

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

请参阅下面使用 SAAJ 的 SOAP Web 服务调用的工作示例(运行它!)。它调用这个网络服务

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/ 。

于 2013-04-11T06:38:32.033 回答
3

或者只是使用Apache CXF 的 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

于 2016-11-18T14:32:56.887 回答
2

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

如果您有来自 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();
    }   
}

}

于 2021-06-24T16:35:56.493 回答
1

我找到了一种更简单的生成肥皂消息的替代方法。给定一个 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
}

下面是一个简单的 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);
  }
}

您可以通过以下方式使用:

 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);
      }
于 2018-12-13T01:34:58.187 回答