`

Create a Web Service with Apache CXF and Jboss6 on MyEclipse

    博客分类:
  • SOA
阅读更多

参考:http://www.celinio.net/techblog/?p=531

 

      I have recently started studying Apache CXF, the open source web service framework. I am familiar with developing Web Services using EJB 3, Axis or Glue. But not with CXF. Until now.
CXF is a mix of two projects : Celtix and XFire, which explains the name CXF.
It provides support for the JAX-WS, JAX-RS and JAX-RPC specifications.
Developing Web Services using CXF and JBoss is quite easy. The only annoying part is to figure out which JARs libraries
to include in the classpath and which JARs libraries to exclude.

So i am developing a simple web service that calculates the BMI (Body Mass Index). The formula is :
BMI = weight / (height x height)

 

1、 First create a Web Project in MyEclipse 8.6.

 

2、Download the CXF framework apache-cxf-2.3.1.zip at http://cxf.apache.org/download.html

 

3、After unzipping the archive, I added to WEB-INF/lib all the libraries that are inside the apache-cxf-2.3.1\lib 

folder. It is of course not a good habit to have since adding all sorts of libraries can produces conflicts.
I had to remove a few jars such as :
jaxb-xjc-2.2.1.1.jar, xalan-2.7.1.jar and serializer-2.7.1.jar

 

 

 

 

There are probably a few extras jars that are not needed but at least they do not produce any error for that simple web service. 

 

4、Create the Service Endpoint Interface (SEI). Here I use the bottom-up approach (code first) : that is first create a Java class that will be converted into a web service. The other approach is Top-Down (contract first, based on an existing WSDL file). 

 

package com.company.bmi.services;
import javax.jws.WebParam;
import javax.jws.WebService;

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

 

 

5、Create the class that implements this interface. It will implement the operations defined by the service : 

 

package com.company.bmi.services;

public class IBMICalculatorImpl implements IBMICalculator{
    @Override
    public double computeBMI(double weight, double height) {
        return weight / (height * height);
    }

 

 6、Add the Spring-based configuration file, cxf.xml, under the package directory, for instance :

 

<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" />

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

 

7、You need to update the deployment descriptor file WEB-INF/web.xml : 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>BMI</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:com/company/bmi/services/cxf.xml</param-value>
  </context-param>

  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>

  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>
        org.apache.cxf.transport.servlet.CXFServlet
    </servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
</web-app>
 

 

I specify the path where to find the cxf.xml file and also the CXFServlet. 

7、Finally create a client that uses this web service. This is a standalone Java class that invokes the IBMICalculator web service : 

 

package com.company.bmi.client;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.company.bmi.services.IBMICalculator;

public final class Client {
    private Client() {
    }

    public static void main(String args[]) throws Exception {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());
        factory.setServiceClass(IBMICalculator.class);
        factory.setAddress("http://localhost:8085/BMI/services/cxfBmi");
        IBMICalculator client = (IBMICalculator) factory.create();
        Double bmi = client.computeBMI(75, 170);
        System.out.println("BMI : " + bmi);
    }
}
 

 

9、run BMI on Jboss As 6

The available SOAP services are listed at the following URL : 

 

http://localhost:8085/BMI/services/

 

10、WSDL : 

http://localhost:8085/soa_apache_cxf_bmi/services/cxfBmi?wsdl 

 

 

<wsdl:definitions name="IBMICalculatorImplService" targetNamespace="http://services.bmi.company.com/">
<wsdl:types>
<xs:schema elementFormDefault="unqualified" targetNamespace="http://services.bmi.company.com/" version="1.0">
<xs:element name="computeBMI" type="tns:computeBMI"/>
<xs:element name="computeBMIResponse" type="tns:computeBMIResponse"/>
<xs:complexType name="computeBMI">
<xs:sequence>
<xs:element name="weight" type="xs:double"/>
<xs:element name="height" type="xs:double"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="computeBMIResponse">
<xs:sequence>
<xs:element name="return" type="xs:double"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="computeBMIResponse">
<wsdl:part element="tns:computeBMIResponse" name="parameters">
    </wsdl:part>
</wsdl:message>
<wsdl:message name="computeBMI">
<wsdl:part element="tns:computeBMI" name="parameters">
    </wsdl:part>
</wsdl:message>
<wsdl:portType name="IBMICalculator">
<wsdl:operation name="computeBMI">
<wsdl:input message="tns:computeBMI" name="computeBMI">
    </wsdl:input>
<wsdl:output message="tns:computeBMIResponse" name="computeBMIResponse">
    </wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="IBMICalculatorImplServiceSoapBinding" type="tns:IBMICalculator">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="computeBMI">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="computeBMI">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="computeBMIResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="IBMICalculatorImplService">
<wsdl:port binding="tns:IBMICalculatorImplServiceSoapBinding" name="IBMICalculatorImplPort">
<soap:address location="http://localhost:8085/BMI/services/cxfBmi"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
 

There is one operation : computeBMI. The input is computeBMI and the output is computeBMIResponse

11、The response, displayed when running the client (java code) :

 

---------------------------
ID: 1
Address: http://localhost:8085/BMI/services/cxfBmi
Encoding: UTF-8
Content-Type: text/xml
Headers: {SOAPAction=[""], Accept=[*/*]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:computeBMI xmlns:ns1="http://services.bmi.company.com/"><weight>75.0</weight><height>170.0</height></ns1:computeBMI></soap:Body></soap:Envelope>
--------------------------------------
30 janv. 2011 17:56:14 org.apache.cxf.interceptor.AbstractLoggingInterceptor log
INFO: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {content-type=[text language="/xml;charset=UTF-8"][/text][/text], Date=[Sun, 30 Jan 2011 16:56:14 GMT], Content-Length=[241], X-Powered-By=[Servlet/3.0; JBossAS-6], Server=[Apache-Coyote/1.1]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:computeBMIResponse xmlns:ns2="http://services.bmi.company.com/"><return>0.0025951557093425604</return></ns2:computeBMIResponse></soap:Body></soap:Envelope>
--------------------------------------
BMI : 0.0025951557093425604			
2
1
分享到:
评论
2 楼 chenwq 2011-04-15  
  Another way to test the Web Service is by using soapUI[http://www.soapui.org/].
  First create a new SoapUi project (Ctrl+n), give
the project a name and add the WSDL file or URL :

 
  Then you need to call the service that you want to test. Double-click the created request “Request 1″ below the computeBMI method. The request editor pops up :


  What you need to do next is add values to the parameters of the method. For instance :
<weight>75</weight><height>170</height>

 
  Then submit the request (green arrow on the top left corner) and validate the response :
<return>0.0025951557093425604</return>

 
1 楼 chenwq 2011-04-14  
Web Project catalog:

相关推荐

    Developing Web Services with Apache CXF and Axis2(3rd Edition).zip

    Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且...

    Developing Web Services with Apache CXF and Axis2, 3rd Edition

    ### 开发Web服务:使用Apache CXF与Axis2 #### 书籍概述 《开发Web服务:使用Apache CXF与Axis2》(第三版)是一本详细介绍如何利用Apache CXF和Axis2开发高质量Web服务的专业书籍。该书由Kent Kaiok Tong编写,并...

    Developing Web Services with Apache CXF and Axis2_3rd Edition

    ### 开发Web服务:使用Apache CXF与Axis2(第三版) #### 一、书籍概述 本书《开发Web服务:使用Apache CXF与Axis2》是针对希望学习如何使用Java创建Web服务的专业人士所编写的实用教程。作者Kent Kai Ok Tong以...

    Apache CXF Web Service Development(源码)

    【标题】中的"Apache CXF Web Service Development"指的是使用Apache CXF框架进行Web服务开发的过程。这通常包括了创建服务接口、实现服务逻辑、配置服务端点、以及发布和调用服务等步骤。源码部分可能包含了示例...

    Restful Web Services with Apache CXF

    Restful Web Service开发可供参考

    实战Web Service —— 使用Apache CXF开发Web服务的教程

    **实战Web Service与Apache CXF开发** Web服务是一种在互联网上进行通信的标准协议,它允许应用程序之间进行数据交换。Apache CXF是一个开源框架,用于构建和部署Web服务,支持多种Web服务标准,如SOAP、RESTful ...

    两本关于apache cxf的书籍,英文

    这两本书籍,"Apache CXF Web Service Development" 和 "Developing Web Services with Apache CXF and Axis2 (3rd Edition)",显然是为了深入理解Apache CXF的工作原理和实际应用而编写的。 第一本书,"Apache CXF...

    WebService with Apache CXF

    Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且...

    apache cxf_jar包

    Apache CXF是一个开源的Java框架,它主要用于构建和开发服务导向架构(SOA)中的Web服务。这个"apache cxf_jar包"包含了实现基于Java的Web服务所需的一系列核心库。下面我们将深入探讨这些jar文件及其在Web服务实现...

    Apache CXF Web Service Development.pdf

    ### Apache CXF Web Service 开发相关知识点 #### 一、Apache CXF简介 Apache CXF 是一个开源项目,提供了一套强大的框架用于构建和开发基于Java的Web服务。它支持多种协议,包括SOAP和REST,并且能够很好地与其他...

    Apache CXF Web service 资料

    详细的从入门到精通, 手把手的教你做WEB SERVICE 该资源借花献佛,是一个高手写的,我在这里借花献佛,推广推广,让大家多一个学习的机会,吃水不忘挖井人,轻大家也谢谢写该文档的高手

    基于Apache CXF构建SOA应用

    Apache CXF 框架是一个比较有前途的开源 Web Services 框架,也是构建 SOA 架构应用的利器。本书采用案例源码和解说形式全面介绍 Apache CXF 框架的功能。 本书共 15 章,大致分为三个部分。第一部分介绍关于 SOA 和...

Global site tag (gtag.js) - Google Analytics