JAX-WS Hello World-Beispiel - Dokumentstil

In diesem Lernprogramm zeigen wir Ihnen, wie Sie mit JAX-WS einen SOAP-basierten Web-Service-Endpunkt (Dokumentstil) erstellen. Vergleiche mit RPC-Stil , es bedarf einiger zusätzlicher Anstrengungen, damit es funktioniert.

Verzeichnisstruktur dieses Beispiels

jaxws-document-hello-world - Beispiel

JAX-WS Web Service End Point

So erstellen Sie einen Dokumentstil-Webdienst in JAX-WS.

1. Erstellen Sie eine Web Service Endpoint-Schnittstelle

Das Annotieren mit @ SOAPBinding ist optional, da der Standardstil "document" ist.

Datei: HelloWorld.java

package com.mkyong.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
//Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.DOCUMENT, use=Use.LITERAL)//optional
public interface HelloWorld{

    @WebMethod String getHelloWorldAsString(String name);

}

2. Erstellen Sie eine Web Service Endpoint-Implementierung

Datei: HelloWorld Impl.java

package com.mkyong.ws;

import javax.jws.WebService;
//Service Implementation
@WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{

    @Override
    public String getHelloWorldAsString(String name) {
        return "Hello World JAX-WS " + name;
    }

}

3. Erstellen Sie einen Endpoint Publisher.

Datei: HelloWorldPublisher.java

package com.mkyong.endpoint;

import javax.xml.ws.Endpoint;
import com.mkyong.ws.HelloWorldImpl;
//Endpoint publisher
public class HelloWorldPublisher{

    public static void main(String[]args) {
       Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
    }

}

Warten Sie, wenn Sie den Endpoint-Publisher ausführen, wird folgende Fehlermeldung angezeigt:

Wrapper class com.mkyong.ws.jaxws.GetHelloWorldAsString is not found.
    Have you run APT to generate them?

Siehe diesen Link://webservices/jax-ws/wrapper-class-package-jaxws-methodname-is-not-found-have-you-run-apt-to-generate-them/[article].

Sie müssen das Tool " wsgen " verwenden, um die erforderlichen tragbaren JAX-WS-Artefakte zu erstellen. Gehen Sie zum nächsten Schritt über.

4. wsgen Befehl

Für den Dokumentstil sind zusätzliche Klassen erforderlich. Sie können mit " wsgen " alle erforderlichen Java-Artefakte (Mapping-Klassen, WSDL- oder XSD-Schema) generieren. Der Befehl " wsgen " ist zum Lesen einer Serviceendpunkt-Implementierungsklasse erforderlich:

wsgen -keep -cp . com.mkyong.ws.HelloWorldImpl

Es werden zwei Klassen generiert. Kopieren Sie es in Ihren Ordner " package.jaxws ".

Datei: Get Hello World String.java

package com.mkyong.ws.jaxws;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "getHelloWorldAsString", namespace = "http://ws.mkyong.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getHelloWorldAsString", namespace = "http://ws.mkyong.com/")
public class GetHelloWorldAsString {

    @XmlElement(name = "arg0", namespace = "")
    private String arg0;

   /** **
     **
     **  @return
     **      returns String
     ** /    public String getArg0() {
        return this.arg0;
    }

   /** **
     **
     **  @param arg0
     **      the value for the arg0 property
     ** /    public void setArg0(String arg0) {
        this.arg0 = arg0;
    }

}

Datei: GetHelloWorldAsStringResponse.java

package com.mkyong.ws.jaxws;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "getHelloWorldAsStringResponse", namespace = "http://ws.mkyong.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getHelloWorldAsStringResponse", namespace = "http://ws.mkyong.com/")
public class GetHelloWorldAsStringResponse {

    @XmlElement(name = "return", namespace = "")
    private String __return;

   /** **
     **
     **  @return
     **      returns String
     ** /    public String getReturn() {
        return this.__return;
    }

   /** **
     **
     **  @param __return
     **      the value for the __return property
     ** /    public void setReturn(String __return) {
        this.__return = __return;
    }

}
  • Hinweis ** Das Tool "wsgen" ist im Ordner "JDK__Path \ bin \" verfügbar. Für Details lesen Sie bitte diesen Link://webservices/jax-ws/jax-ws-wsgen-tool-example/[JAX-WS: wsgen-Toolbeispiel].

5. Fertig

Fertig, veröffentlichen und testen Sie es per URL:

http://localhost : 9999/ws/hallo? wsdl .

Web Service Client

Erstellen Sie einen Webdienstclient, um auf Ihren veröffentlichten Dienst zuzugreifen.

Datei: Hello World Client.java

package com.mkyong.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.mkyong.ws.HelloWorld;

public class HelloWorldClient{

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

    URL url = new URL("http://localhost:9999/ws/hello?wsdl");
        QName qname = new QName("http://ws.mkyong.com/", "HelloWorldImplService");

        Service service = Service.create(url, qname);

        HelloWorld hello = service.getPort(HelloWorld.class);

         System.out.println(hello.getHelloWorldAsString("mkyong"));

    }

}

Ausgabe

Hello World JAX-WS mkyong

Verfolgung des SOAP-Verkehrs

Zeigt von oben nach unten an, wie der SOAP-Umschlag zwischen Client und Server in diesem Dokumentstil-Webdienst fließt.

1. Fordern Sie eine WSDL-Datei an

Zunächst sendet der Client eine WSDL-Anforderung an den Serviceendpunkt:

  • Client senden Anfrage: **

GET/ws/hello?wsdl HTTP/1.1
User-Agent: Java/1.6.0__13
Host: localhost:9999
Accept: text/html, image/gif, image/jpeg, ** ; q=.2, ** /** ; q=.2
Connection: keep-alive
  • Server Antwort senden: **

HTTP/1.1 200 OK
Transfer-encoding: chunked
Content-type: text/xml;charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
    <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net.
        RI's version is JAX-WS RI 2.1.1 in JDK 6. -->
    <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net.
        RI's version is JAX-WS RI 2.1.1 in JDK 6. -->
<definitions
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:tns="http://ws.mkyong.com/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    targetNamespace="http://ws.mkyong.com/"
    name="HelloWorldImplService">
<types>
<xsd:schema>
    <xsd:import namespace="http://ws.mkyong.com/"
        schemaLocation="http://localhost:9999/ws/hello?xsd=1"></xsd:import>
</xsd:schema>
</types>

<message name="getHelloWorldAsString">
    <part name="parameters" element="tns:getHelloWorldAsString"></part>
</message>
<message name="getHelloWorldAsStringResponse">
    <part name="parameters" element="tns:getHelloWorldAsStringResponse"></part>
</message>

<portType name="HelloWorld">
    <operation name="getHelloWorldAsString">
        <input message="tns:getHelloWorldAsString"></input>
        <output message="tns:getHelloWorldAsStringResponse"></output>
    </operation>
</portType>

<binding name="HelloWorldImplPortBinding" type="tns:HelloWorld">

    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document">
    </soap:binding>
    <operation name="getHelloWorldAsString">
        <soap:operation soapAction=""></soap:operation>
        <input>
            <soap:body use="literal"></soap:body>
        </input>
        <output>
            <soap:body use="literal"></soap:body>
        </output>
    </operation>

</binding>

<service name="HelloWorldImplService">

<port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding">

<soap:address location="http://localhost:9999/ws/hello"></soap:address>

</port>
</service>
</definitions>

2. getHelloWorldAsString (Stringname)

Bei einem zweiten Aufruf, einer Client-Put-Methode, wird die Anforderung in einem SOAP-Umschlag aufgerufen und an den Serviceendpunkt gesendet. Rufen Sie am Serviceendpunkt die angeforderte Methode auf, legen Sie das Ergebnis in einen SOAP-Umschlag und senden Sie es an den Client zurück.

  • Client senden Anfrage: **

POST/ws/hello HTTP/1.1
SOAPAction: ""
Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, ** ; q=.2, ** /** ; q=.2
Content-Type: text/xml; charset=utf-8
User-Agent: Java/1.6.0__13
Host: localhost:9999
Connection: keep-alive
Content-Length: 224

<?xml version="1.0" ?>
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <S:Body>
            <ns2:getHelloWorldAsString xmlns:ns2="http://ws.mkyong.com/">
                <arg0>mkyong</arg0>
            </ns2:getHelloWorldAsString>
        </S:Body>
    </S:Envelope>
  • Server Antwort senden: **

HTTP/1.1 200 OK
Transfer-encoding: chunked
Content-type: text/xml; charset=utf-8

<?xml version="1.0" ?>
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <S:Body>
            <ns2:getHelloWorldAsStringResponse xmlns:ns2="http://ws.mkyong.com/">
                <return>Hello World JAX-WS mkyong</return>
            </ns2:getHelloWorldAsStringResponse>
        </S:Body>
    </S:Envelope>