`

[Tutorial] RESTful web services with CXF

    博客分类:
  • J2EE
阅读更多
转自: 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 :


  • 大小: 19 KB
分享到:
评论

相关推荐

    Java Tutorial: Creating Web Services

    WSDL(Web Services Description Language)是一种 XML 格式的文档,用于描述 Web 服务的接口和绑定。WSDL 包含三个主要部分: - **接口(Interface)**:定义了服务的抽象操作和消息。 - **绑定(Binding)**:指定...

    The Java™ Web Services Tutorial

    ### Java™ Web Services 教程知识点详解 #### 一、教程概述 《Java™ Web Services 教程》是一本指导开发者使用 Java Web Services Developer Pack(Java WSDP)开发 Web 应用程序的专业指南。该教程由 Sun ...

    Java Web Services Tutorial

    ### Java Web Services 教程知识点解析 #### 一、Java Web Services 概述 Java Web Services 是一种构建可互操作的分布式应用系统的方法。通过利用Java技术,开发人员可以创建和部署符合工业标准且能够在互联网上...

    Mastering Go Web Services

    Effectively deploy and integrate Go web services with applications in the real worldFamiliarize yourself with RESTful practices and apply them in GoA comprehensive tutorial with lots of tips and ...

    The Java Web Services Tutorial

    在Java中,通常使用JAX-RS(Java API for RESTful Web Services)来实现REST服务。 6. **JAX-RS**:JAX-RS提供了简单的注解,如`@Path`、`@GET`、`@POST`等,使得开发者可以在Java类和方法上声明RESTful资源的URI...

    AWS Amazon Web Services Tutorial The Ultimate Beginners Guide azw3

    AWS Amazon Web Services Tutorial The Ultimate Beginners Guide 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Interoperable Python ZSI WSDL SOAP Web Services tutorial

    Interoperable Python ZSI WSDL SOAP Web Services tutorial

    AWS Amazon Web Services Tutorial The Ultimate Beginners Guide epub

    AWS Amazon Web Services Tutorial The Ultimate Beginners Guide 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    The Java EE 6 Tutorial Basic Concepts 4th Edition

    Chapter 13: Building RESTful Web Services with JAX-RS 219 What Are RESTful Web Services? 219 Creating a RESTful Root Resource Class 220 Example Applications for JAX-RS 235 Further Information ...

    AJAX_tutorial05_Web_Services_with_MS_Ajax_cs.pdf

    本文档主要探讨了如何在ASP.NET AJAX框架中利用Web服务(Web Services)实现跨平台的数据交换功能。通过介绍基本概念、配置方法以及具体实现细节,帮助读者更好地理解如何利用ASP.NET AJAX进行动态数据注入或从前端...

    Analysis Services Tutorial SQL Server 2012

    《SQL Server 2012 Analysis Services教程》是针对数据分析师和数据库管理员的一份详尽指南,旨在深入理解和掌握微软的商务智能工具——Analysis Services。SQL Server 2012版本引入了许多增强功能,使得数据处理、...

    Ruby on Rails教程:学习使用Rails进行Web开发Ruby on Rails Tutorial: Learn Web Development with Rails

    本书教您如何使用Ruby on Rails开发和部署真正的,具有工业实力的Web应用程序,Ruby on Rails是为诸如Twitter,Hulu,GitHub和Yellow Pages等顶级网站提供支持的开源Web框架。

    Web Services开发

    通过学习《Java Web Services Tutorial》,我们可以了解到Java Web Services的基础概念、核心技术以及开发流程。这对于构建可靠的、可扩展的企业级应用具有重要意义。掌握这些知识将有助于开发者更好地利用Java技术...

    Ruby on Rails Tutorial - Learn Web Development with Rails 3rd

    3. **Restful 设计**:Rails 鼓励采用 RESTful 架构风格进行设计,这有助于创建符合 Web 标准且易于理解的应用程序接口(API)。 4. **测试驱动开发**:Rails 支持并鼓励使用测试驱动开发(Test-Driven Development,...

    An-Introductory-Tutorial-on-Web-Services--Java-an_java web

    Java Web Services and XML

Global site tag (gtag.js) - Google Analytics