Souvent, JAX-WS fait toujours partie de votre application Web Java. Nous vous montrons ici comment intégrer facilement JAX-WS à une application Web Java.
1. Dossier de projet
Commencez par examiner la structure de ce dossier de projet.
2. Service Web
Un service web super simple. Le code est explicite.
File: HelloWorld.java
package com.mkyong.ws; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public class HelloWorld{ @WebMethod(operationName="getHelloWorld") public String getHelloWorld(String name) { return "Hello World JAX-WS " + name; } }
3. Descripteur de déploiement de service Web (sun-jaxws.xml)
Créez un descripteur de déploiement de service Web, nommé
sun-jaxws.xml
.
File: sun-jaxws.xml
<?xml version="1.0" encoding="UTF-8"?> <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0"> <endpoint name="HelloWorldWs" implementation="com.mkyong.ws.HelloWorld" url-pattern="/hello"/> </endpoints>
4. Descripteur de déploiement d’applications Web (web.xml)
Dans le standard
web.xml
,
-
Définit
“` Com.sun.xml.ws.transport.http.servlet.WSServletContextListener` ”comme classe d’écouteur.
-
Définit «` com.sun.xml.ws.transport.http.servlet.WSServlet` »comme votre
service Web (bonjour) servlet.
Fichier: web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app__2__3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <listener> <listener-class> com.sun.xml.ws.transport.http.servlet.WSServletContextListener </listener-class> </listener> <servlet> <servlet-name>hello</servlet-name> <servlet-class> com.sun.xml.ws.transport.http.servlet.WSServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
5. Fait
L’intégration entre JAX-WS et l’application Web est terminée. Déployez-le et accédez-y via l’URL: http://localhost : 8080/WebServicesExample/hello
Télécharger le code source
Téléchargez-le - lien://wp-content/uploads/2011/03/JAX-WS-Integrate-WebApplication-Example.zip[JAX-WS-Integrate-WebApplication-Example.zip](7 Ko)
Référence
-
lien://services web/jax-ws/deploy-jax-ws-services-web-sur-tomcat/[Comment
déployer JAX-WS sur Tomcat]. lien://services web/jax-ws/jax-ws-bonjour-monde-exemple-document-style/[JAX-WS
bonjour exemple du monde (style de document)]
lien://étiquette/intégration/[intégration]lien://étiquette/java/[java]lien://tag/jax-ws/[jax-ws]lien://tag/web-services/[services web]