一、配置环境变量(Windows系统下要重启)

1、JAVA_HOME即JDK安装路径bin上一级,java -version命令验证

2、CXF_HOME即cxf安装路径bin上一级,cxf解压包下有samples文件夹,下有java_first_pojo文件夹,此处shift+右键打开命令窗口,ant server启动案例WebService服务,如果报错版本问题,打开samples下文件common_build.xml添加上面的JDK版本(如<equals arg1="${ant.java.version}" arg2="1.7"/>),ant client启动客户端

3、ANT_HOME即ant安装路径bin上一级,ant -version命令验证

二、发布WebService服务

Demo1、ServerFactoryBean类这种方式创建出的WSDL文档名字不符合开发规范,一般不用

  1、新建PersonService类

package com.hjp.server;

public class PersonService {

    public String sayHello(String name){
return "Hello "+name;
} }

PersonService

  2、创建发布服务类,记得添加cxf安装文件下lib下jar包

package com.hjp.server;

import org.apache.cxf.frontend.ServerFactoryBean;

public class Publisher {

    public static void main(String[] args) {
//创建cxf发布的服务对象
ServerFactoryBean serverFactoryBean=new ServerFactoryBean();
//设置服务的类
serverFactoryBean.setServiceClass(PersonService.class);
//设置服务地址
serverFactoryBean.setAddress("http://localhost:5555/hello");
//设置服务对象
serverFactoryBean.setServiceBean(new PersonService());
//发布
serverFactoryBean.create();
} }

Publisher

Demo2、JaxWsServerFactoryBean类这种方式结合@WebService注解,WSDL文档名称比较规范

  1、新建PersonService1类

package com.hjp.server;

import javax.jws.WebService;

@WebService
public class PersonService1 { public String sayHello(String name){
return "Hello "+name;
} }

PersonService1

  2、创建发布服务类

package com.hjp.server;

import org.apache.cxf.frontend.ServerFactoryBean;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean; public class Publisher1 { public static void main(String[] args) {
//创建cxf发布的服务对象
JaxWsServerFactoryBean serverFactoryBean=new JaxWsServerFactoryBean();
//设置服务的类
serverFactoryBean.setServiceClass(PersonService1.class);
//设置服务地址
serverFactoryBean.setAddress("http://localhost:5555/hello");
//设置服务对象
serverFactoryBean.setServiceBean(new PersonService1());
//发布
serverFactoryBean.create();
} }

Publisher1

Demo3、JaxWsServerFactoryBean发布接口类服务,@WebService注解加在接口上,在实现类上方法无效

  1、新建UserService接口及其实现类UserServiceImpl

package com.hjp.server;

import javax.jws.WebService;

@WebService
public interface UserService { public String sayHello(String name); }

UserService

package com.hjp.server;

public class UserServiceImpl implements UserService {
@Override
public String sayHello(String name) {
return "Hello "+name;
}
}

UserServiceImpl

  2、创建发布服务类

package com.hjp.server;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class Publisher2 {

    public static void main(String[] args) {
//创建cxf发布的服务对象
JaxWsServerFactoryBean serverFactoryBean=new JaxWsServerFactoryBean();
//设置服务的接口类
serverFactoryBean.setServiceClass(UserService.class);
//设置服务地址
serverFactoryBean.setAddress("http://localhost:5555/hello");
//设置服务对象
serverFactoryBean.setServiceBean(new UserServiceImpl());
//发布
serverFactoryBean.create();
} }

Publisher2

三、WebService服务客户端调用

1、使用wsimport命令方式,省略

2、使用cxf中wsdl2java命令(wsimport不支持soap12协议下生成客户端代码)

wsdl2java -d . -p com.hjp.stub.soap12 http://localhost:5555/hello?wsdl

-d生成的客户端代码存放路径,点代表当前路径;-p 第一个参数是生成客户端代码的包路径,第二个参数是wsdl访问地址;如果省略第一个参数,默认包路径为xmlns:tns后面域名倒序

附SOAP12协议代码:

package com.hjp.server;

import javax.jws.WebService;
import javax.xml.ws.BindingType; import static javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING; @WebService
//默认是SOAP11,如果使用SOAP12,需要使用BindingType注解
@BindingType(value = SOAP12HTTP_BINDING)
public interface UserService { public String sayHello(String name); }

UserService接口

四、配置CXFServlet发布服务

1、新建JavaWeb项目

2、面向发布普通类的服务

新建PersonService类

package com.hjp.server;

import javax.jws.WebService;

@WebService
public class PersonService { public String sayHello(String name){
return "Hello "+name;
} }

PersonService

在WEB-INF下新建lib文件夹,将cxf所有相关jar包引入

在WEB-INF下新建cxf-servlet.xml文件,配置如下

<?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"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
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
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<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"/> <!--
endpoint:发布类的形式的服务配置
address:配置时前必须加/
implementor:具体的服务类
-->
<jaxws:endpoint id="personService" address="/personService" implementor="com.hjp.server.PersonService">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:endpoint>
</beans>

cxf-servlet.xml

在web.xml中配置CXFServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> <servlet>
<servlet-name>mycxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>mycxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>

web.xml

访问地址:http://localhost:8080/services如下图,点击WSDL链接,指向WSDL地址

3、面向发布接口的服务

新建接口及其实现类

package com.hjp.server;

import javax.jws.WebService;

@WebService
public interface UserService {
public String sayHello(String name);
}

UserService

package com.hjp.server;

public class UserServiceImpl implements UserService {
@Override
public String sayHello(String name) {
return "Hello "+name;
}
}

UserServiceImpl

修改cxf-servlet.xml配置文件

<?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"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
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
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<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"/> <!--
endpoint:发布类的形式的服务配置
address:配置时前必须加/
implementor:具体的服务类
-->
<jaxws:endpoint id="personService" address="/personService" implementor="com.hjp.server.PersonService">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:endpoint> <!--
server:发布接口类形式服务
serviceClass:接口类
-->
<jaxws:server id="userService" address="/userService" serviceClass="com.hjp.server.UserService">
<!--配置接口实现类-->
<jaxws:serviceBean>
<bean class="com.hjp.server.UserServiceImpl"></bean>
</jaxws:serviceBean> <jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:server>
</beans>

cxf-servlet.xml

访问地址:http://localhost:8080/services如下图,点击相应WSDL链接,指向相应WSDL地址

五、WebService发布方式和客户端代码生成方式相互独立,只要有WSDL地址,就可以用某种客户端代码生成工具生成客户端代码

WebService之CXF的更多相关文章

  1. WebService之CXF注解报错(一)

    WebService之CXF注解 1.详细报错例如以下 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] ...

  2. WebService它CXF注释错误(两)

    WebService它CXF注解 1.详细报错例如以下 五月 04, 2014 11:24:12 下午 org.apache.cxf.wsdl.service.factory.ReflectionSe ...

  3. WebService之CXF注解报错(三)

    WebService之CXF注解 1.具体错误如下 五月 04, 2014 11:29:28 下午 org.apache.cxf.wsdl.service.factory.ReflectionServ ...

  4. WebService之CXF注解报错(二)

    WebService之CXF注解 1.具体报错如下 五月 04, 2014 11:24:12 下午 org.apache.cxf.wsdl.service.factory.ReflectionServ ...

  5. 转载 WebService 的CXF框架 WS方式Spring开发

    WebService 的CXF框架 WS方式Spring开发   1.建项目,导包. 1 <project xmlns="http://maven.apache.org/POM/4.0 ...

  6. 【WebService】WebService之CXF和Spring整合(六)

    前面介绍了WebService与CXF的使用,项目中我们经常用到Spring,这里介绍CXF与Spring整合 步骤 1.创建一个Maven Web项目,可以参照:[Maven]Eclipse 使用M ...

  7. 转-JAVA webservice之CXF 范例--http://cxshun.iteye.com/blog/1275408

    JAVA webservice之CXF 博客分类: j2ee相关 昨天我们一起学习了一下xfire,今天我们来看一下CXF,为什么学完那个接着学这个呢.因为CXF是在xfire的基础上实现 的,所以我 ...

  8. Webservice与CXF框架快速入门

    1. Webservice Webservice是一套远程调用技术规范 远程调用RPC, 实现了系统与系统进程间的远程通信.java领域有很多可实现远程通讯的技术,如:RMI(Socket + 序列化 ...

  9. WebService之CXF框架

    本文主要包括以下内容 ant工具的使用 利用cxf实现webservice cxf与spring整合 ajax访问webservice ant 工具 1.为什么要用到ant这个工具呢? Ant做为一种 ...

  10. So easy Webservice 7.CXF 发布WebService

    (一)使用ServerFactoryBean 方式实现发布WS服务 1.新建项目,添加cxf jar包到项目中 2.编写服务实现类 /** * CXF WebService * 不用注解 * @aut ...

随机推荐

  1. Servlet 全局验证是否登录

    Java过滤器的使用和拦截排除的实现 http://blog.csdn.net/kyunchen/article/details/52187514

  2. JS 二维数组排序

    <script> //测试方法 var a = [ {name:'hdj', age:128}, {name:'hdj1', age:28}, {name:'hdj1', age:78}, ...

  3. React Native布局

    一款好的APP离不了一个漂亮的布局,本文章将向大家分享React Native中的布局方式FlexBox. 在React Native中布局采用的是FleBox(弹性框)进行布局. FlexBox提供 ...

  4. Ubuntu下Ruby的下载和编译源码安装

    1.Ruby的下载 Ruby可以在Ruby 官网上下载,如果想获取更多的Ruby版本,可以到淘宝镜像网站下载. 2.Ruby的编译源码安装 解压 首先把下载下来的源码压缩包解压到自己指定的目录 编译安 ...

  5. Ext3.4实现增删查改(form版)

    var TaskPolicyStore = new Ext.data.JsonStore( {    autoLoad : false,    url : 'PolicyServlet?method= ...

  6. (笔记)Mysql命令update set:修改表中的数据

    update set命令用来修改表中的数据. update set命令格式:update 表名 set 字段=新值,… where 条件; 举例如下:mysql> update MyClass ...

  7. 第三百六十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的基本查询

    第三百六十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的基本查询 1.elasticsearch(搜索引擎)的查询 elasticsearch是功能 ...

  8. e787. 用JSpinner实现小时选择

    // Create a calendar object and initialize to a particular hour if desired Calendar calendar = new G ...

  9. Linq to Entity 动态拼接查询条件(重点是OR)

    public static class PredicateExtensions { /// <summary> /// 机关函数应用True时:单个AND有效,多个AND有效:单个OR无效 ...

  10. (实用)Ubuntu 、CentOS更换国内源

    Ubuntu更换apt-get源 通过编辑/etc/apt/sources.list文件,我们能够更换Ubuntu的默认软件更新源.通常是将其换成一些国内比较知名的源.本文主要列举这些内容. 注意,在 ...