其实webService的发布不仅仅只有xfire,今天,给大家介绍一下用CXF发布一个webService的小demo,CXF也是我做webService用的第一个框架。。。
先将相关的jar引进来,在pom文件中添加
<dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>2.7.3</version>
  </dependency>
  
  <dependency>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-rt-frontend-jaxws</artifactId>
   <version>2.7.3</version>
  </dependency>
即可引入相关的
第一步,不废话,还是写一个简单的接口与实现类,代码如下
public interface SayCXFService {
public  String sayHello(String name);
}
public class SayCXFServiceImpl implements SayCXFService{
@Override
public String sayHello(String name) {
  return name +" hello,this is my first CXF webService!";
}
}
接下来就是重点了,配置文件了,配置文件如下。相应的解释也有
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd
                        http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd"
default-lazy-init="true"
>
  <!-- cxf 相关配置,这个照搬就行了,cxf框架里面自带的配置 -->
<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" />

<!-- service定义 -->
<bean id="sayCXFbean"
  class="com.mip.biz.webservice.cxf.server.service.impl.SayCXFServiceImpl"
  scope="singleton"
/>
<!--
serviceClass 表示你要对外开发的接口,address表示的地址,本质就是一个servletName,可以随便命名
   -->
<jaxws:server id="webService" serviceClass="com.mip.biz.webservice.cxf.server.service.SayCXFService" address="/server">
     <!-- 表示你的接口实现类的bean -->
  <jaxws:serviceBean>
   <ref bean="sayCXFbean"/>
  </jaxws:serviceBean>
</jaxws:server>
</beans>

然后是web.xml文件添加如下内容
<servlet>
  <description>apache cxf 配置 webservice 服务</description>
  <servlet-name>CXFService</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>CXFService</servlet-name>
  <url-pattern>/CXFService/*</url-pattern>
</servlet-mapping>
除了测试类,似乎和前段时间的xfire没啥区别,试一下启动tomcat,会发现抱NullPointerException吧
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webService': Invocation of init method failed; nested exception is java.lang.NullPointerException
别急,你只需要在接口上稍微改动一下即可

//注解的方式,将接口注入,targetNamespace中的内容其实就是将包名写反就行了。。这也是我们平时包名的命名规则类似。com开头,然后模块名称..有么像www.ho123.com?
@WebService(targetNamespace = "http://service.server.cxf.webservice.biz.mip.com/")
public interface SayCXFService {
public  String sayHello(String name);
}
//实现类
@WebService(endpointInterface="com.mip.biz.webservice.cxf.server.service.SayCXFService",
  targetNamespace = "http://service.server.cxf.webservice.biz.mip.com/")
public class SayCXFServiceImpl implements SayCXFService{
@Override
public String sayHello(String name) {
  return name +" hello,this is my first CXF webService!";
}
}
好了重启不会报错了
然后在浏览器中输入http://localhost:8080/mip/CXFService/server?wsdl出现祖国江山一片红的景象就ok了
- <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://service.server.cxf.webservice.biz.mip.com/" elementFormDefault="unqualified" targetNamespace="http://service.server.cxf.webservice.biz.mip.com/" version="1.0">
<xs:element name="sayHello" type="tns:sayHello" />

<xs:element name="sayHelloResponse" type="tns:sayHelloResponse" />

- <xs:complexType name="sayHello">
+ <xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string" />

</xs:sequence>

</xs:complexType>

- 。。。。。。
注意到上面的红色标记部分没,其实我们可以将参数的名字显示出来,改下接口
@WebService(targetNamespace = "http://service.server.cxf.webservice.biz.mip.com/")
public interface SayCXFService {
@WebMethod
@WebResult
//下面的name="wsdl中暴露出来的接口参数名称"
public  String sayHello(@WebParam(name="name",targetNamespace = "http://service.server.cxf.webservice.biz.mip.com/") String name);
}
重新看下wsdl文件:

- <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://service.server.cxf.webservice.biz.mip.com/" elementFormDefault="unqualified" targetNamespace="http://service.server.cxf.webservice.biz.mip.com/" version="1.0">
<xs:element name="sayHello" type="tns:sayHello" />

<xs:element name="sayHelloResponse" type="tns:sayHelloResponse" />

- <xs:complexType name="sayHello">
- <xs:sequence>
<xs:element form="qualified" minOccurs="0" name="name" type="xs:string" />

</xs:sequence>

</xs:complexType>

-

以上是用java开发的服务端,下一章我们接着写客户端与测试类
@WebService(targetNamespace = "http://service.server.cxf.webservice.biz.mip.com/")
public interface SayCXFClient {
public  String sayHello(String name); }
客户端其实直接将服务端拷贝过来就可以了。。。。当然这是在java开发的服务端基础上你才可以做,假如你的webservice服务端是其他语言开发的,,,那么可以用其他方法生成客户端咯,主要依据的是wsdl文件。。这个我在下一篇博客给大家介绍。。。请期待
最后在写个测试类
public class Test {
public static void main(String[] args) {
  JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
  factory.setServiceClass(SayCXFClient.class);
  factory.setAddress("http://localhost:8080/mip/CXFService/server");
  SayCXFClient service = (SayCXFClient) factory.create();
       System.out.println(service.sayHello("zhouxy"));
}
}
运行输出
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Unmarshalling Error: unexpected element (uri:"", local:"arg0"). Expected elements are <{http://service.server.cxf.webservice.biz.mip.com/}name>
这个是由于我们在服务端明确指出了参数名name照成的,别担心,改下就行了
@WebService(
targetNamespace = "http://service.server.cxf.webservice.biz.mip.com/")
public interface SayCXFClient {
@WebMethod
@WebResult
//下面的name="wsdl中暴露出来的接口参数名称"
public  String sayHello(@WebParam(name="name",targetNamespace = "http://service.server.cxf.webservice.biz.mip.com/") String name);
}
继续运行输出:
zhouxy hello,this is my first CXF webService!
ok,demo结束,更详细的理论知识自己查资料去吧

[置顶] 利用CXF发布webService的小demo的更多相关文章

  1. CXF发布webService服务以及客户端调用

    这篇随笔内容是CXF发布webService服务以及客户端调用的方法 CXF是什么? 开发工作之前需要下载CXF和安装 下载地址:http://cxf.apache.org 安装过程: <1&g ...

  2. SpringBoot整合cxf发布webService

    1. 看看项目结构图 2. cxf的pom依赖 1 <dependency>2 <groupId>org.apache.cxf</groupId>3 <art ...

  3. SpringMVC4整合CXF发布WebService

    SpringMVC4整合CXF发布WebService版本:SpringMVC 4.1.6,CXF 3.1.0项目管理:apache-maven-3.3.3 pom.xml <project x ...

  4. Spring集成CXF发布WebService并在客户端调用

    Spring集成CXF发布WebService 1.导入jar包 因为官方下载的包里面有其他版本的sprring包,全导入会产生版本冲突,所以去掉spring的部分,然后在项目根目录下新建了一个CXF ...

  5. 使用CXF发布WebService服务简单实例

    一.说明: 前面介绍了使用axis2来发布Webservice服务,现在介绍一种更popular,更高效的Webservice服务发布技术:CXF Apache CXF = Celtix + XFir ...

  6. 使用CXF发布WebService

    这里普及一下WebService和cxf的知识.关于webservice和cxf:   WebService.各种提供服务的组件     .企业总线.通讯总线(ESB)CXF:是一个SOA框架,Axi ...

  7. CXF发布webservice入门

    1.设置CXF的bin目录进环境变量 2.CXF导入相关的jar包. 3.建立接口 @WebService public interface HelloWorld { public void say( ...

  8. cxf发布 webservice服务

    导包 antlr-2.7.7.jar aopalliance-1.0.jar asm-3.3.jar commons-collections-3.2.1.jar commons-lang-2.6.ja ...

  9. 使用CXF发布webservice服务及注意要点

    一.概念 1.什么是webservice Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML标准来描述.发布.发现.协调和配置这些应用程序,用 ...

随机推荐

  1. 14.5.1 Resizing the InnoDB System Tablespace

    14.5.1 Resizing the InnoDB System Tablespace 本节描述如何增加或者减少InnoDB 系统表空间的大小 增加InnoDB 系统表空间的大小 最简单的方式增加I ...

  2. 实例:怎样使用 Netty 下载文件

    本实例主要參考的是官网的examples:点击这里 使用场景:client向Netty请求一个文件,Netty服务端下载指定位置文件到client. 本实例使用的是Http协议,当然,能够通过简单的改 ...

  3. 简单深刻:为控件创建MouseEnter和MouseLeave事件(覆盖WndProc,增加对消息的处理,真简单!)——连对CM_MOUSEENTER的消息处理都是颇有深意啊!

    其实很简单: unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, D ...

  4. C语言笔记之结构体

    结构的本质是C语言的一种数据抽象,通俗的说,是基本数据类型的重组. 为什么要重组呢?由于基本数据类型不够用了.为什么不够用了呢?由于须要的信息类型太多了. 这是一个非常大的话题.信息本来是没有什么类型 ...

  5. Conexant声卡实现内录功能(win7)

    Conexant声卡本身没有立体声混音设备可选,所以我们采用virtual audio device,实现内录功能. [1]下载virtual audio device.下载地址:http://dow ...

  6. Cocos2dx3.4 VS2013无法打开包括文件extensions/ExtensionExport.h解决的方法

    本来打算看白鹭引擎的.可是又被叫回来研究新引擎呢,不搞不知道,一搞发现cocos2dx新版本号3.4又有了一些变化. 我查了网上的资源,都没有解决的方法.我如今应该是第一个出这个问题的解决的方法哦.. ...

  7. poj1860(spfa判正环)

    题目连接:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rat ...

  8. hdu2569(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2569 分析: f(n),n个珠子的合格数:a(n),n个珠子,最后2个相同的合格数:b(n),n个珠子 ...

  9. hdu 4925 贪心 自己从小到大做数据找方法规律

    http://acm.hdu.edu.cn/showproblem.php?pid=4925 自己逐个做数据找规律.提供下我的找的: 1 2 1 3 2 2 2 3 3 3 然后发现这种矩阵是最优的: ...

  10. ActiveMQ源码架构解析第一节(转)

    工作四年已久,也快到了而立之年,本人也酷爱技术,总是想找一些途径来提升自己,想着温故而知新所以就写起了博客,然而写博客这个想法也是酝酿了很久,近期也看到了有很多人在问关于ActiveMQ的相关问题,有 ...