Dans ce didacticiel, nous vous montrons comment utiliser JAX-WS pour créer un point de terminaison de service Web basé sur SOAP (style de document). Comparez avec le lien://webservices/jax-ws/exemple-hello-world-example/[style RPC], des efforts supplémentaires sont nécessaires pour que cela fonctionne.
Structure de répertoire de cet exemple
Point de terminaison du service Web JAX-WS
Voici les étapes à suivre pour créer un service Web de style document dans JAX-WS.
1. Créer une interface de noeud final de service Web
En fait, annoté avec @ SOAPBinding est optionnel, car le style par défaut est document.
File: 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. Créer une implémentation de noeud final de service Web
File: HelloWorldImpl.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. Créez un éditeur de point de terminaison.
File: 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()); } }
Attendez, lorsque vous exécutez l’éditeur du point de terminaison, vous obtiendrez le message d’erreur suivant:
Wrapper class com.mkyong.ws.jaxws.GetHelloWorldAsString is not found. Have you run APT to generate them?
Voir ce lien://services Web/jax-ws/wrapper-class-package-jaxws-methodname-is-not-found-have-you-run-apt-to-generate-them/[article].
Vous devez utiliser l’outil “ wsgen ” pour générer les artefacts portables JAX-WS nécessaires. Laissez passer à l’étape suivante.
4. Commande wsgen
Le style de document nécessite l’exécution de classes supplémentaires. Vous pouvez utiliser « wsgen » pour générer tous les artefacts Java nécessaires (classes de mappage, schéma wsdl ou xsd). La commande “ wsgen ” est requise pour lire une classe d’implémentation de noeud final de service:
wsgen -keep -cp . com.mkyong.ws.HelloWorldImpl
Il va générer deux classes, copiez-le dans votre dossier « package.jaxws ».
File: GetHelloWorldAsString.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; } }
File: 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; } }
-
Remarque ** L’outil “wsgen” est disponible dans le dossier “JDK__Path \ bin \”. Pour plus de détails, veuillez lire ce lien://webservices/jax-ws/exemple-outil jax-ws-wsgen/[article JAX-WS: exemple d’outil wsgen]
Client de service Web
Créez un client de service Web pour accéder à votre service publié.
File: HelloWorldClient.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")); } }
Sortie
Hello World JAX-WS mkyong
Traçage du trafic SOAP
De haut en bas, montrant comment l’enveloppe SOAP circule entre le client et le serveur dans ce service Web de style document.
1. Demander un fichier WSDL
En premier lieu, le client envoie une demande wsdl au noeud final de service:
-
Demande d’envoi du client: **
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
-
Réponse d’envoi du serveur: **
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 (nom de chaîne)
Un second appel, une demande d’appel de méthode de vente du client dans une enveloppe SOAP, et l’envoyer au point de terminaison du service. Au niveau du noeud final de service, appelez la méthode demandée, insérez le résultat dans une enveloppe SOAP et renvoyez-la au client.
-
Demande d’envoi du client: **
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>
-
Réponse d’envoi du serveur: **
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>
Télécharger le code source
Téléchargez-le - lien://wp-content/uploads/2010/11/JAX-WS-HelloWorld-Document-Example.zip[JAX-WS-HelloWorld-Document-Example.zip](10 Ko)
lien://tag/hello-world/[hello world]lien://tag/jax-ws/[jax-ws]lien://tag/web-services/[services web]