Créer et publier un Webservice avec CXF et Maven
Nous allons voir dans ce billet comment créer et publier un service sur tomcat avec maven et CXF.
mvn archetype:create -DgroupId=com.test.ws.cxf -DartifactId=cxf-webapp -DarchetypeArtifactId=maven-archetype-webapp
cd ./cxf-webapp
mvn clean
Editer le fichier pom.xml afin d'y rajouter les dépendances suivantes :
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.3.1</version>
</dependency>
Créer un WSDL, nous resterons classique et ferons un simple service HelloWorld :
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://com.test.ws/hello/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="hello" targetNamespace="http://com.test.ws/hello/">
<wsdl:types>
<xsd:schema targetNamespace="http://com.test.ws/hello/">
<xsd:element name="HelloWorld">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="in" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="HelloWorldResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="out" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="HelloWorldRequest">
<wsdl:part element="tns:HelloWorld" name="parameters"/>
</wsdl:message>
<wsdl:message name="HelloWorldResponse">
<wsdl:part element="tns:HelloWorldResponse" name="parameters"/>
</wsdl:message>
<wsdl:portType name="hello">
<wsdl:operation name="HelloWorld">
<wsdl:input message="tns:HelloWorldRequest"/>
<wsdl:output message="tns:HelloWorldResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="helloSOAP" type="tns:hello">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="HelloWorld">
<soap:operation soapAction="http://com.test.ws/hello/HelloWorld"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/> </wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="hello">
<wsdl:port binding="tns:helloSOAP" name="helloSOAP">
<soap:address location="http://www.example.org/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
Placer le WSDL dans WEB-INF/wsdl/hello.wsdl
A présent nous allons générer le code java grâce au plugin maven de CXF. Pour celà nous allons rajouter ceci au fichier pom :
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/webapp/WEB-INF/wsdl/hello.wsdl</wsdl>
<extraargs>
<extraarg>-impl</extraarg>
<extraarg>-wsdlLocation</extraarg>
<extraarg>/WEB-INF/wsdl/hello.wsdl</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Exécuter la commande suivante :
mvn generate-sources
Le code généré se trouve dans /target/generated/cxf.
Ajouter le code généré à votre répertoire sources, par convention /src/main/java, et faire les modifications voulues dans HelloImpl.java. C'est dans cette classe que sera implémenté le service.
Il ne reste plus qu'a publier le service. Pour ceci CXF propose deux alternatives, mais la seconde ne semble pas opérationnelle.
Voici donc comment doit être renseigné votre 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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value> /WEB-INF/beans.xml </param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
Enfin il ne reste plus qu'a déclarer les services dans spring (CXF comporte des dépendances vers spring, aucune librairie supplémentaire n'a à être déclarée dans maven). Créer le fichier WEB-INF/beans.xml :
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:endpoint id="helloBean" implementor="ws.test.com.hello.HelloImpl" address="/helloService" endpointName="e:helloSOAP" serviceName="e:hello" xmlns:e="http://com.test.ws/hello/" wsdlLocation="WEB-INF/wsdl/hello.wsdl"/
</beans>
Plus qu'a packager :
mvn package
Le war est prés à être déployé dans tomcat.
have fun ;)
Placer le WSDL dans WEB-INF/wsdl/hello.wsdl
A présent nous allons générer le code java grâce au plugin maven de CXF. Pour celà nous allons rajouter ceci au fichier pom :
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/webapp/WEB-INF/wsdl/hello.wsdl</wsdl>
<extraargs>
<extraarg>-impl</extraarg>
<extraarg>-wsdlLocation</extraarg>
<extraarg>/WEB-INF/wsdl/hello.wsdl</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Exécuter la commande suivante :
mvn generate-sources
Le code généré se trouve dans /target/generated/cxf.
Ajouter le code généré à votre répertoire sources, par convention /src/main/java, et faire les modifications voulues dans HelloImpl.java. C'est dans cette classe que sera implémenté le service.
Il ne reste plus qu'a publier le service. Pour ceci CXF propose deux alternatives, mais la seconde ne semble pas opérationnelle.
Voici donc comment doit être renseigné votre 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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value> /WEB-INF/beans.xml </param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
Enfin il ne reste plus qu'a déclarer les services dans spring (CXF comporte des dépendances vers spring, aucune librairie supplémentaire n'a à être déclarée dans maven). Créer le fichier WEB-INF/beans.xml :
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:endpoint id="helloBean" implementor="ws.test.com.hello.HelloImpl" address="/helloService" endpointName="e:helloSOAP" serviceName="e:hello" xmlns:e="http://com.test.ws/hello/" wsdlLocation="WEB-INF/wsdl/hello.wsdl"/
</beans>
Plus qu'a packager :
mvn package
Le war est prés à être déployé dans tomcat.
have fun ;)
Commentaires
Enregistrer un commentaire