一、配置环境变量(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. java json转换

    https://blog.csdn.net/WillJGL/article/details/77866224 SpringBoot中如果需要实现json的序列化和反序列化,我们会使用json解析工具. ...

  2. [Qt] 利用QtWebKit完成JavaScript访问C++对象

    一. 介绍 在浏览器扩展或者WebApp的项目经常用的脚本语言javascript有很多局限性,比如,javascript语言不能够夸窗口访问js对象,不能直接读写磁盘文件(这个也正是发明人设计的安全 ...

  3. [转]浅谈Android五大布局(一)——LinearLayout、FrameLayout和AbsoulteLayout

    Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.Android的五大布局分别是LinearLay ...

  4. 使用tomcat搭建centos的yum源

    最近在折腾大数据,需要搭建一个yum源.一般的做法是在CentOS中安装httpd,然后将rpm包放入/var/www/html下面,再执行[createrepo .]即可. 不过虚拟机对传文件终归是 ...

  5. Java数组排序和搜索

    如何排序数组并搜索其中的元素? 以下示例显示如何使用sort()和binarySearch()方法来完成任务.用户定义的方法printArray()用于显示数组输出: package com.yiib ...

  6. andorid ndk 各种坑啊 记录下

    android jni代码回调java的问题 因为多线程原因会导致找不到java类,无法call函数的问题 问题1找不到java类 在JNI_OnLoad的时候 保存下来 JNIEXPORT jint ...

  7. EF5+MVC4系列(11)在主视图中用Html.RenderPartial调用分部视图(ViewDate传值);在主视图中按钮用ajax调用子action并在子action中使用return PartialView返回分布视图(return view ,return PartialView区别)

    一:主视图中使用Html.RenderPartial来调用子视图(注意,这里是直接调用子视图,而没有去调用子Action ) 在没有使用母版页的主视图中(也就是设置了layout为null的视图中), ...

  8. Druid搭配log4j2输出SQL语句和结果

    一.引言 其实Druid的内置了log4jdbc来显示SQL语句,虽然显示效果不如原生的log4jdbc效果好,但是因为内置所以不需要其他更多的配置. 二.使用 1. 创建基于druid的logger ...

  9. Java常量池的理解

    1.常量池的好处常量池是为了避免频繁的创建和销毁对象而影响系统性能,其实现了对象的共享.例如字符串常量池,在编译阶段就把所有的字符串文字放到一个常量池中.(1)节省内存空间:常量池中所有相同的字符串常 ...

  10. Opengl的gl_NormalMatrix

    原文地址:http://blog.csdn.net/ichild1964/article/details/9728357 参考:http://www.gamedev.net/topic/598985- ...