Create a Web Service with Apache CXF and Jboss6 on MyEclipse

技术标签: Web  Apache  Myeclipse  SOAP  Servlet

参考: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			

来源:网络


智能推荐

Spring 4 集成Apache CXF开发JAX-RS Web Service

什么是JAX-RS 在JSR-311规范中定义,即Java API for RESTful Web Services,一套Java API,用于开发 RESTful风格的Webservice。 工程概况 主要功能:用户增删改查controller(not RESTful,懒得改了) + 查询用户webservice(RESTful) 框架:spring+spring mvc+mybatis 数据库...

Create a cloud ready web application with Sprin...

为什么80%的码农都做不了架构师?>>>    #Create a cloud ready web application with Spring Roo# Spring Roo is a rapid scollad tool brought by SpringSource, which can help you to create a data center...

opencv学习第6课官方练习实现 Create a Paint application with adjustable colors and brush radius using trackbars

 练习题目来源(网址最下方):https://docs.opencv.org/4.1.0/d9/dc8/tutorial_py_trackbar.html   参考内容: https://blog.csdn.net/Ibelievesunshine/article/details/89351928 https://blog.csdn.net/Ibelievesunshine/a...

Cannot create a secure XMLInputFactory, you should either add woodstox or set org.apache.cxf.stax.al

            tomcat升级为8后,webservice接口调用报错,具体异常信息如下:“Cannot create a secure XMLInputFactory,you should either add woodstox or set org.apache.cxf.stax.allowInsecurePar...

myeclipse中生成web service时出现异常Unable to create JAXBContext

尝试着用myeclipse生成web service,但总是报Unable to create JAXBContext错误 错误信息如下图: 解决方法: 其实仔细阅读一下堆栈信息就会发现,说 ResultSet是一个接口,而JAXB不能处理接口。 记得网上有人说 JAX-WS只支持基本数据类型,int、String等, 其实不是的,JAX支持的数据类型很多,...

猜你喜欢

CXF与Spring构建web service

目录 pom.xml web.xml applicationContext.xml class TestService.java TestServiceImpl.java Structure SoapUI测试 Create New SOAP Project Name The Project And Enter WSDL Address pom.xml web.xml applicationCont...

CXF开发RESTful风格的Web Service

http://www.itnose.net/detail/6160699.html 一、配置Spring的配置文件 1、需要引入新的 jar 包。 2、配置applicationContext-server.xml 文件。使用jaxrs:server ,记得引入jaxrs 的schema 约束。 1)address:为地址,如http://localhost:8080/Java_WS_Server...

002.Create a web API with ASP.NET Core MVC and Visual Studio for Windows -- 【在windows上用vs与asp.net co...

索引: 目录索引 Create a web API with ASP.NET Core MVC and Visual Studio for Windows 在windows上用vs与asp.net core mvc 创建一个 web api 程序 2017-5-24 8 分钟阅读时长  本文内容 1.Overview 综述 2.Create the project 创建一个项目...

Web Service学习-CXF开发Web Service实例demo

Web Service是什么?    Web Service不是框架,更甚至不是一种技术。而是一种跨平台,跨语言的规范   Web Service解决什么问题:       为了解决不同平台,不同语言所编写的应用之间如何调用问题。例如,有一个C语言写的程序,它想去调用java语言写的某个方法。   集中解决:1,远程调用 2,跨...

Eclipse中创建一个简单的Maven项目(详细)

前提条件:Eclipse已经整合了Maven。 简单配置Maven 已经配置好的,请跳过 配置Maven的路径: window - preferences 找到Maven展开 点击ADD   在弹出的对话框中点击 Directory,选择Maven的路径,选择到Maven的根目录即可,不需要到bin目录!! 勾选新添加的Maven安装路径,点击Apply 配置Maven的仓库 ...

问答精选

C++ NetBeans - Removing Console from Win32

I have a Win32 app that displays a console window in the back. How can I, using NetBeans/C++, remove this console window? Thanks in advance. you might want to go for "Right-Mouse-Button: Properti...

Node losing gravity after SCNAction

I'd like to drop an object and then move it back to the top and let it fall again. The first part is working, but then the node seems to lose its gravity and isn't falling again. It looks like its phy...

PyQt5 store time of keyPressEvent

Issue: I have a program where I will be showing several pages with a stacked widget, and users will have to press a button (using code I've written below) to go to the next page of the stacked widget....

How to initialize a systemc port name which is an array?

I wanted to initialize a port name. The port is an array and my code does not work. The code below would work by giving clk with a name "clk". However clk port is not an array: How do I name...

Java source code can not be found in eclipse

In an xpages application a javav source code was added to the Local folder within the Lotus nsf file. Now can not be seen, and can not be found with search. The code still woking, but it is not possib...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答