[Tutorial] RESTful web services with CXF

转自: http://www.celinio.net/techblog/?p=637

This is a short follow-up to a previous post about Web Services with CXF.
Developing RESTful web services with CXF is quite easy. Here I quickly explore 2 ways to do it.


1) HTTP Binding :

One way to do it is to use HTTP Binding as described here : https://cwiki.apache.org/CXF20DOC/http-binding.html

The spring bean xml configuration file is modified to add the bindingUri parameter and also the  JaxWsServiceFactoryBean factory :

cxf.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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://cxf.apache.org/jaxws
 http://cxf.apache.org/schemas/jaxws.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" />
 <import resource="classpath:META-INF/cxf/cxf-extension-http-binding.xml"/>

 <jaxws:endpoint id="calcBMI"
 implementor="com.company.bmi.services.IBMICalculatorImpl"
 address="/cxfBmi"
 bindingUri="http://apache.org/cxf/binding/http">
 <jaxws:serviceFactory>
 <bean>
 <property name="wrapped" value="true" />
 </bean>
 </jaxws:serviceFactory>
 </jaxws:endpoint>
 
</beans>


Then add to the interface the annotations from the Java Rest Annotations codehaus project :
IBMICalculator.java:
package com.company.bmi.services;

import javax.jws.WebParam;
import javax.jws.WebService;
import org.codehaus.jra.Get;
import org.codehaus.jra.HttpResource;

@WebService
public interface IBMICalculator {
 @Get
 @HttpResource(location = "/computeBMI/{weight}/{height}")
 public  double computeBMI(@WebParam(name="weight") double weight, @WebParam(name="height") double height) ;
}


In the JBoss console, we can observe that the mapping is done :

20:21:59,250 INFO  [ReflectionServiceFactoryBean] Creating Service {http://services.bmi.company.com/}IBMICalculatorImplService from class com.company.bmi.services.IBMICalculator
20:21:59,469 INFO  [JRAStrategy] Mapping method computeBMI to resource /computeBMI/{weight}/{height} and verb GET
20:21:59,512 INFO  [ServerImpl] Setting the server's publish address to be /cxfBmi
20:21:59,528 INFO  [ContextLoader] Root WebApplicationContext: initialization completed in 710 ms


However, as stated in CXF web site,  the “binding has been deprecated and is likely to be removed from CXF in one of its future releases.” So it’s best to use a different way before it’s too late !

2) JAX-RS

An alternative way is to use the implementation of the JAX-RS api provided by CXF. So all you need to do is annotate the class with the JAX-RS annotations and modify the spring configuration file (called cxf.xml in the example of my previous post).

cxf.xml :
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:jaxrs="http://cxf.apache.org/jaxrs"
 xmlns:jaxws="http://cxf.apache.org/jaxws"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://cxf.apache.org/jaxrs
 http://cxf.apache.org/schemas/jaxrs.xsd
 http://cxf.apache.org/jaxws
 http://cxf.apache.org/schemas/jaxws.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" />
 <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>

<jaxws:endpoint id="calcBMI"
 implementor="com.company.bmi.services.IBMICalculatorImpl"
 address="/cxfBmi"  />

 <jaxrs:server id="bmiservice" address="/">
 <jaxrs:serviceBeans>
 <ref bean="IBMICalculatorImpl"/>
 </jaxrs:serviceBeans>
 </jaxrs:server>
 <bean id="IBMICalculatorImpl"/>

</beans>


And annotate the class that implements the interface :
IBMICalculatorImpl.java :
package com.company.bmi.services;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Produces("text/plain")
public class IBMICalculatorImpl implements IBMICalculator{

 //@Override
 @GET
 @Path("/computeBMI/{weight}/{height}")
 public double computeBMI(@PathParam("weight") double weight, @PathParam("height") double height) {
 return weight / (height * height);
 }
}


IBMICalculatorImpl computeBMI(weight, height) is mapped to an HTTP GET request on /computeBMI/{weight}/{height}. This request contains 2 parameters : weight and height.

The data format, definied with the annotation @Produces, is plain text but it could be XML, JSON…

So we see that very little is needed to transform the IBMICalculator service to make it available in the form of an HTTP GET method at the following URL : http://localhost:8085/BMI/services/computeBMI/75/170 . The result is displayed in plain text :

[Tutorial] RESTful web services with CXF_第1张图片

你可能感兴趣的:(spring,Web,xml,webservice,jboss)