参考官网文档:http://cxf.apache.org/docs/writing-a-service-with-spring.html

从官网上下载 cxf 的包,包里会有 samples 文件夹,该文件夹中存放的就是cxf 的一些小例子

这里就是针对 java_first_spring_support 例子的改写 与 说明,该例子是采用 spring +maven +cxf 技术

创建项目

  • 使用Eclipse 创建一个 Maven Project,工程名为 TestCXFDome1 ,修改pom.xml 引入需要使用的jar 包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>panie</groupId>
<artifactId>TestCXFDome1</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TestCXFDome1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<cxf.version>3.1.6</cxf.version>
<springframework.version>4.1.9.RELEASE</springframework.version>
</properties> <dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>TestCXFDome1</finalName>
</build>
</project>

编写服务端

  • 首先写一个接口。@WebService注解表示是要发布的web服务
package demo.spring.service;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
String sayHi(String text);
}
  • 对该接口写一个实现类。@WebService注解表示是要发布的web服务,endpointInterface参数的值是该服务类对应的接口。
package demo.spring.service;

import javax.jws.WebService;

@WebService(endpointInterface = "demo.spring.service.HelloWorld")
public class HelloWorldImpl implements HelloWorld { public String sayHi(String text) {
System.out.println("sayHi called");
return "Hello " + text;
}
}
  • 声明 server bean

  CXF 支持 Spring 2.0 Schema 标签配置方式,并且提供快捷暴露 Web Services 的标签。对于 JAX_WS 端,我们可以使用<jaxws:endpoint> 来设置服务器端的 endpoint (请参考英文原文)

  1)我们需要引入 Spring 与 CXF 的命名空间(namespace),这样,我们可以使用 Spring 与 CXF 的标签配置了。  2)我们需要引入我们所需要的 CXF 的 Bean 定义文件

  3)定义我们具体实现的 Bean

  在 WEB-INF 目录下创建一个 cxf-servlet.xml 文件来声明 一个endpoint bean

<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-servlet.xml"/>
<jaxws:endpoint id="helloWorld" implementor="demo.spring.service.HelloWorldImpl" address="/HelloWorld"/>
</beans>

  如果 想引入 spring 来管理bean,可以这样写

<bean id="hello" class="demo.spring.service.HelloWorldImpl" />

<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />

  id - 指在 spring context 中的bean id

  implementor - 指定 实现类。当使用bean 来表示实现类时,使用 #bean-name

  address - 指 服务将会被发布的位置,这里只能使用相对路径。因为CXF 不知道 war 名字,也不知道 容器端口,CXF 运行时将会更新 endpoint 地址

  • 配置Servlet

  如果采用 默认路径的 cxf-servlet.xml ,在web.xml 中配置

    <servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

  如果 cxf-servlet.xml  更换的位置 或者 名字,如改为 beans.xml ,则需要在 web.xml 中配置

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/beans.xml</param-value>
</context-param> <listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

部署到web服务器上,发布webservice工程,输入http://localhost:8080/TestCXFDome1/HelloWorld?wsdl

编写客户端(一)

  在客户端可以使用 <jaxws:client> 来声明。你只需要提供 bean name,服务接口,服务请求URL,它就可以在远程SOAP服务器上创建一个指定名称的bean来实现服务接口。

  • 配置一个 client-bean.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/schema/jaxws.xsd">
<bean id="client" class="demo.spring.service.HelloWorld" factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="demo.spring.service.HelloWorld"/>
<property name="address" value="http://localhost:8080/TestCXFDome1/HelloWorld"/>
</bean>
</beans>
  • 编写一个 测试类 Client
package demo.spring.client;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import demo.spring.service.HelloWorld;

public final class Client {

    private Client() {
} public static void main(String args[]) throws Exception {
// START SNIPPET: client
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"}); HelloWorld client = (HelloWorld)context.getBean("client"); String response = client.sayHi("Joe");
System.out.println("Response: " + response);
System.exit(0);
// END SNIPPET: client
}
}

运行 测试类,即可看到 调用 服务的结果

编写客户端(二)

package demo.spring.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import demo.spring.service.HelloWorld;

public class Client2
{
public static void main(String[] args)
{
//创建WebService客户端代理工厂
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//注册WebService接口
factory.setServiceClass(HelloWorld.class);
//设置WebService地址
factory.setAddress("http://localhost:8180/TestCXFDome1/HelloWorld");
HelloWorld hello = (HelloWorld)factory.create();
//调用webservice接口方法
String response = hello.sayHi("panie");//返回sucess
System.out.println("Response: " + response);
}
}

使用CXF 来发布一个 service的更多相关文章

  1. Spring整合CXF,发布RSETful 风格WebService(转)

    Spring整合CXF,发布RSETful 风格WebService 这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有很大一部分都是一样的.关于发布CXF WebServer和Sp ...

  2. Windows Azure Cloud Service (40) 使用VS2013的publishSettings文件,发布Cloud Service

    <Windows Azure Platform 系列文章目录> 在之前的文档中,笔者已经介绍了如何使用本地证书上传至云端的方式,将本地的Cloud Service发布至云端. 在本章中,笔 ...

  3. tomcat发布web service教程

    这几天一直在准备找工作,自学了关于web service的一些基本的内容,也遇到了不少问题.现在就把我自己学到的知识和大家分享一下,由于是初学,所以有什么错误的地方请大家帮忙指正,感激不尽~~!! 1 ...

  4. 新手Axis2 发布Web Service之路

    由于公司的需求,需要写几个银行接口写模拟器(Mock Server),此次接口需要发布成一个WEB Service. 一开始,我以为只要负责写接口的业务层就行了,具体的框架或是环境搭建可以不用管.在与 ...

  5. Java发布一个简单 webservice应用 并发送SOAP请求

    一.创建并发布一个简单的webservice应用 1.webservice 代码: package com.ls.demo; import javax.jws.WebMethod; import ja ...

  6. 利用VS2008发布一个简单的webservice

    一个开发好的webservice,怎样发布出去,供其他电脑访问呢? 本文将介绍如何发布一个简单的webservice,其中的内容都是在网上查看别人文章,自己仿照着做了一遍,因此,难免会发生错误,如果发 ...

  7. WebGIS实现在线要素编辑之ArcGIS Server 发布Feature Service 过程解析

    WebGIS实现在线要素编辑之ArcGIS Server 发布Feature Service 过程解析 FeatureService也称要素服务,其最大的好处就是支持在线要素编辑,并将编辑同步更新到后 ...

  8. 使用CXF+Spring发布WebService,启动报错

    使用CXF+Spring发布WebService,启动报错,日志如下: 五月 12, 2017 9:01:37 下午 org.apache.tomcat.util.digester.SetProper ...

  9. 使用Advanced Installer 13.1打包发布 Windows Service服务程序

    原文: 使用Advanced Installer 13.1打包发布 Windows Service服务程序 项目中需要用到一个定时推送案件状态的需求,本人小菜一只,在同事建议下要写成一个windows ...

随机推荐

  1. dp入门问题

    昨天晚上的rank彻底废了..一个星期没敲代码完全没手感.作为总结,贴一道昨天浪费了我两小时的dp.http://acm.dirring.com/problem.php?cid=1003&pi ...

  2. poj1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12507   Accepted: 6070 Des ...

  3. Android应用性能测试

    Android应用性能测试 Android用户也许会经常碰到以下的问题: 1)应用后台开着,手机很快没电了——应用耗电大 2)首次/非首次启动应用,进入应用特别慢——应用启动慢 3)应用使用过程中,越 ...

  4. Android webview通过http get下载文件下载两次的问题及解决方法

    一.现象 一般通过Android webview进行下载文件的方法是 1.重写DownloadListener的onDownloadStart方法,在onDownloadStart方法中弹出对话框提示 ...

  5. Smoothing in fMRI analysis (FAQ)

    Source: http://mindhive.mit.edu/node/112 1. What is smoothing? "Smoothing" is generally us ...

  6. eclipse/intellij idea 远程调试hadoop 2.6.0

    很多hadoop初学者估计都我一样,由于没有足够的机器资源,只能在虚拟机里弄一个linux安装hadoop的伪分布,然后在host机上win7里使用eclipse或Intellj idea来写代码测试 ...

  7. 利用Spring的@Async异步处理改善web应用中耗时操作的用户体验

    Web应用中,有时会遇到一些耗时很长的操作(比如:在后台生成100张报表再呈现,或 从ftp下载若干文件,综合处理后再返回给页面下载),用户在网页上点完按钮后,通常会遇到二个问题:页面超时.看不到处理 ...

  8. 批处理将字符串输出到Windows剪贴板

    批处理将字符串输出到Windows剪贴板 2016-06-30 23:29 339人阅读 评论(0) 收藏 举报 版权声明:作者:N3verL4nd 出处:http://blog.csdn.net/x ...

  9. 0 bug 读后感

    本书全名是 <0 bug- C/C++商用工程之道>,这是一本有争议的书,豆瓣链接: http://book.douban.com/subject/4149139/ ,建议有一些商用的开发 ...

  10. href的那些事

    很多网站中都会使用<a>标签和 href属性来做链接,尤其在分页显示中用得最普遍.然而很多人对href的使用却并不十分了解. 1.href="#" 这个在网页中上滚回顶 ...