Introduction
De manière simplifiée, SOAP permet de faire de l'appel de procédure à distance au dessus du protocole HTTP. Les données échangées sont au format XML.
Apache CXF est un framwork Java qui permet de simplifier la creation de Webservices SOAP
Architechture
L'idée est de faire un package pour le client (un jar), un package pour le serveur (un war), et un package commun (jar) aux deux qui contiendra les interfaces et les POJO échangés entre les deux services.
Cela simplifie la création d'un client en Java, mais cela n'empêche pas de créer un webservice simplement à partir du WSDL qui sera généré à la demande par CXF.
Dépendances Maven pour CXF
02 | < groupId >javax.servlet</ groupId > |
03 | < artifactId >servlet-api</ artifactId > |
04 | < version >2.5</ version > |
05 | < scope >provided</ scope > |
09 | < groupId >org.apache.cxf</ groupId > |
10 | < artifactId >cxf-rt-frontend-jaxws</ artifactId > |
11 | < version >2.1.3</ version > |
15 | < groupId >org.apache.cxf</ groupId > |
16 | < artifactId >cxf-rt-transports-http</ artifactId > |
17 | < version >2.1.3</ version > |
Package Commun
Tout d'abord l'interface du service
01 | package org.djo.example; |
03 | import javax.jws.WebParam; |
04 | import javax.jws.WebResult; |
05 | import javax.jws.WebService; |
08 | public interface MonService { |
10 | @WebResult (name = "helloresponse" ) |
11 | Hello sayHello( @WebParam (name = "name" ) String name); |
Puis une classe POJO simple pour le retour. Ce n'est pas obligatoire, on aurait pu faire un return String tout simple pour notre méthode sayHello.
01 | package org.djo.example; |
11 | public Date getWhen() { |
15 | public void setWhen(Date when) { |
19 | public String getHello() { |
23 | public void setHello(String hello) { |
Package serveur
Implémentation du service
01 | package org.djo.example; |
03 | public class MonServiceImpl implements MonService { |
05 | public Hello sayHello(String name) { |
06 | Hello response = new Hello(); |
07 | response.setHello( "Hello " + name); |
WEB-INF/web.xml
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
03 | PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" |
07 | < param-name >contextConfigLocation</ param-name > |
08 | < param-value >WEB-INF/beans.xml</ param-value > |
13 | org.springframework.web.context.ContextLoaderListener |
19 | < servlet-name >CXFServlet</ servlet-name > |
20 | < display-name >CXF Servlet</ display-name > |
22 | org.apache.cxf.transport.servlet.CXFServlet |
24 | < load-on-startup >1</ load-on-startup > |
29 | < servlet-name >CXFServlet</ servlet-name > |
30 | < url-pattern >/services/*</ url-pattern > |
WEB-INF/beans.xml
Ce fichier va être chargé automatiquement par Spring (dont dépend CXF), afin de déclarer nos services.
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
07 | < import resource = "classpath:META-INF/cxf/cxf.xml" /> |
08 | < import resource = "classpath:META-INF/cxf/cxf-extension-soap.xml" /> |
09 | < import resource = "classpath:META-INF/cxf/cxf-servlet.xml" /> |
11 | < jaxws:endpoint id = "MonService" implementor = "org.djo.example.MonServiceImpl" |
12 | address = "/MonService" /> |
Execution
Un fois le package déployé, le WSDL du service sera disponible par exemple à l'URL http://localhost:8080/service.server/services/MonService?wsdl
Package Client
Le client aura besoin d'importer le package commun, qui contient la déclaration de l'interface et les pojo partagés. Bien sur, il est possible de générer ces pojo et cette interface à partir du WSDL, mais ce n'est pas l'objet de cet article.
Le client est alors aussi simple que cela:
01 | package org.djo.example; |
03 | import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; |
05 | public class TestService { |
09 | public static MonService getMonService() { |
10 | JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean(); |
11 | factoryBean.setServiceClass(MonService. class ); |
12 | factoryBean.setAddress(address); |
13 | MonService monService = (MonService) factoryBean.create(); |
17 | public static void main(String[] args) { |
18 | Hello response = getMonService().sayHello( "WAJDI" ); |
19 | System.out.println(response.getHello()); |