-->

lundi 19 janvier 2015

CXF3 and Spring 4 : from the begining ...

Introduction :

In our development, we often need to make web services if either SOAP or REST, one of the best frameworks that not only implements the standard JEE but offers many more possibilities for the web services dev is apache-cxf.

Personally, there are more than two years that I use apache-cxf to develop web services, but recently when I had to push things, I discovered how to do many other features with this framework that I would like to share on my blog.

Now, let's code :

So first we need to create a web project with maven: for this is simple just run this command on a system console :

mvn archetype:generate 
-DgroupId=fr.mjhazbri.webservices 
-DartifactId=ShowInterceptor 
-DarchetypeArtifactId=maven-archetype-webapp 
-DinteractiveMode=false

Now our project is created, but contains no great thing: neither config spring nor the config cxf. So we're going to edit web.xml : 

 
  Apache CXF binding
  cxf
  org.apache.cxf.transport.servlet.CXFServlet
  1
 
 
  cxf
  /*
 

Then we will create a file named cxf-servlet.xml in the same place as web.xml, so under WEB-INF directory, this file will contain the cxf and spring configuration, it look like this: 


 
 
 
  
 
 

 

Then we will create a file named cxf-servlet.xml in the same place as web.xml, so under WEB-INF directory, this file will contain the cxf and spring configuration, it look like this:  Now we should edit the pom file file to get cxf, spring dependencies, so we will add this to our pom.xml file (we must put these lines between the dependencies tags ):
  
   org.springframework
   spring-web
   4.0.5.RELEASE
   compile
  
  
   org.springframework
   spring-context
   4.0.5.RELEASE
   compile
  
  
   org.apache.cxf
   cxf-api
   2.7.14
   compile
  
  
   org.apache.cxf
   cxf-rt-frontend-jaxws
   2.7.14
   compile
  
  
   org.apache.cxf
   cxf-rt-transports-http
   2.7.14
   compile
  
  
   org.springframework
   spring-test
   4.0.5.RELEASE
   test
  
  
   junit
   junit
   4.8.2
   test
  
  
   log4j
   log4j
   1.2.17
  
Be careful before you have to update the list of repositories, it will look like this :
 
  
   springsource-repo
   SpringSource Repository
   http://repo.springsource.org/release
  
  
   maven2-repository.java.net
   Java.net Repository for Maven
   http://download.java.net/maven/2/
   default
  

First, the class Parameter in the package fr.mjhazbri.webservices.schema should be created to represent input data to our web service :
/**
 * 
 */
package fr.mjhazbri.webservices.schema;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author jhazbri
 * 
 */
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Parameter {
 private String name;

 /**
  * @return the name
  */
 public String getName() {
  return name;
 }

 /**
  * @param name
  *            the name to set
  */
 public void setName(String name) {
  this.name = name;
 }

 @Override
 public String toString() {
  return "Parameter [name=" + name + "]";
 }

}

After this class, in the package fr.mjhazbri.webservices.api, we will create the interface : HelloWorld

package fr.mjhazbri.webservices.api;

import javax.jws.WebService;

import fr.mjhazbri.webservices.schema.Parameter;

/**
 * @author jhazbri
 *
 */
@WebService
public interface HelloWorld {
 
 String sayHello (Parameter parameter );

}

The class that implements HelloWorld interface will be in the fr.mjhazbri.webservices package and named HelloWorldImpl : it look like this :
package fr.mjhazbri.webservices;

import javax.jws.WebService;

import org.apache.log4j.Logger;

import fr.mjhazbri.webservices.api.HelloWorld;
import fr.mjhazbri.webservices.schema.Parameter;

@WebService(endpointInterface = "fr.mjhazbri.webservices.api.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
 private static final Logger logger = Logger.getLogger(HelloWorldImpl.class);
 @Override
 public String sayHello(Parameter parameter) {
  logger.debug("calling sayHello with parameter : "+ parameter);
  return "Hello : " + parameter.getName();
 }
}

Finally, you can now deploy to web container and use your web service.

Aucun commentaire :

Enregistrer un commentaire