[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 !

[b]2) JAX-RS[/b]

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 :

[img]http://dl.iteye.com/upload/attachment/447516/0898fda8-1461-3bb1-803c-1c86f19554e0.png[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值