Voici un guide pour vous montrer comment intégrer Spring à JAX-WS, comme indiqué dans ce lien: http://jax-ws-commons.java.net/spring/ . À la fin de ce didacticiel, vous créerez un service Web HelloWorld simple (JAX-WS) et un bean dans le service Web via Spring.
1. Dossier de projet
Voir la structure finale du dossier de projet.
2. Dépendances du projet
Utilisez Maven pour obtenir toutes les dépendances de la bibliothèque. La clé pour intégrer Spring à JAX-WS est via jaxws-spring.jar .
Fichier: pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4__0__0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mkyong</groupId> <artifactId>WebServicesExample</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>WebServicesExample Maven Webapp</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>java.net</id> <url>http://download.java.net/maven/2</url> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Spring framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> </dependency> <!-- JAX-WS --> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> <version>2.2.3</version> </dependency> <!-- Library from java.net, integrate Spring with JAX-WS --> <dependency> <groupId>org.jvnet.jax-ws-commons.spring</groupId> <artifactId>jaxws-spring</artifactId> <version>1.8</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </exclusion> <exclusion> <groupId>com.sun.xml.stream.buffer</groupId> <artifactId>streambuffer</artifactId> </exclusion> <exclusion> <groupId>org.jvnet.staxex</groupId> <artifactId>stax-ex</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <finalName>web services</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
3. JAX-WS Hello World
Un exemple simple de JAX-WS et une dépendance injectent (DI) «HelloWorldBo» via Spring.
File: HelloWorldWS.java
package com.mkyong.ws; import javax.jws.WebMethod; import javax.jws.WebService; import com.mkyong.bo.HelloWorldBo; @WebService public class HelloWorldWS{ //DI via Spring HelloWorldBo helloWorldBo; @WebMethod(exclude=true) public void setHelloWorldBo(HelloWorldBo helloWorldBo) { this.helloWorldBo = helloWorldBo; } @WebMethod(operationName="getHelloWorld") public String getHelloWorld() { return helloWorldBo.getHelloWorld(); } }
4. Haricots
Voici la classe HelloWorldBo, avec une méthode
getHelloWorld ()
pour renvoyer une chaîne simple.
Fichier: Hello World.java
package com.mkyong.bo; public interface HelloWorldBo{ String getHelloWorld(); }
File: HelloWorldBoImpl.java
package com.mkyong.bo.impl; import com.mkyong.bo.HelloWorldBo; public class HelloWorldBoImpl implements HelloWorldBo{ public String getHelloWorld(){ return "JAX-WS + Spring!"; } }
5. Configuration du Spring Beans
Fichier de configuration Spring Beans permettant de lier le modèle d’URL “ /hello ” à la classe de service Web “ com.mkyong.ws.HelloWorldWS ”.
File: applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ws="http://jax-ws.dev.java.net/spring/core" xmlns:wss="http://jax-ws.dev.java.net/spring/servlet" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://jax-ws.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd" > <wss:binding url="/hello"> <wss:service> <ws:service bean="#helloWs"/> </wss:service> </wss:binding> <!-- Web service methods --> <bean id="helloWs" class="com.mkyong.ws.HelloWorldWS"> <property name="helloWorldBo" ref="HelloWorldBo"/> </bean> <bean id="HelloWorldBo" class="com.mkyong.bo.impl.HelloWorldBoImpl"/> </beans>
-
Remarque Avec ce mécanisme d’intégration jaxws-spring, le fichier sun-jaxws.xml ** n’est plus nécessaire.
6. web.xml
Dans web.xml, déclare «` com.sun.xml.ws.transport.http.servlet.WSSpringServlet` »et le lie à«
hello
».
<web-app id="WebApp__ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app__2__4.xsd"> <display-name>Spring + JAX-WS</display-name> <servlet> <servlet-name>jaxws-servlet</servlet-name> <servlet-class> com.sun.xml.ws.transport.http.servlet.WSSpringServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>jaxws-servlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <!-- Register Spring Listener --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
7. Démo
Démarrez le projet et accédez au service Web déployé via l’URL " /hello ", par exemple http://localhost : 8080/WebServicesExample/hello? Wsdl .
Téléchargez-le - lien://wp-content/uploads/2011/03/JAX-WS-Spring-Integration-Example.zip[JAX-WS-Spring-Integration-Example.zip](10 Ko)
Référence
-
lien://services web/jax-ws/exemple d’intégration d’intégration d’application web/jax-ws/[JAX-WS
Exemple d’intégration d’applications Web Java]. http://jax-ws-commons.java.net/spring/
lien://tag/intégration/[intégration]lien://tag/jax-ws/[jax-ws]lien://tag/spring/[printemps]lien://tag/web-services/[services web]