源码下载地址: http://dl.dbank.com/c0vhvbrxlr
本文来自我的CSDN博客,转载请标明出处:http://blog.csdn.net/cuiran/archive/2010/12/02/6049645.aspx
一.开发环境
我的开发环境是:
MyEclipse 6.5
Tomcat 6.0
Apache cxf-2.3.0 下载地址: http://www.apache.org/dist/cxf/2.3.0/
二.开发步骤
创建Server
1. 将下载的cxf-2.3.0 解压。然后在MyEclipse里面添加一个
User Library名字叫library-cxf-2.3.0 把 lib下的包都添加进去。
2.新建一个Web工程cxfdemo,并添加library-cxf-2.3.0,然后建个com.demo包。在包里面创建HelloWorld接口
代码如下
view plaincopy to clipboardprint?
/**
* HelloWorld.java
* 版权所有(C) 2010 cuiran2001@163.com
* 创建:崔冉 Dec 2, 2010 9:47:44 AM
*/
package com.demo;
import javax.jws.WebService;
/**
* @author 崔冉
* @version 1.0.0
* @desc 接口HelloWorld
*/
@WebService
public interface HelloWorld {
String sayHi(String text);
}
/**
* HelloWorld.java
* 版权所有(C) 2010 cuiran2001@163.com
* 创建:崔冉 Dec 2, 2010 9:47:44 AM
*/
package com.demo;
import javax.jws.WebService;
/**
* @author 崔冉
* @version 1.0.0
* @desc 接口HelloWorld
*/
@WebService
public interface HelloWorld {
String sayHi(String text);
}
再创建一个类实现HelloWorld接口
view plaincopy to clipboardprint?
/**
* HelloWorldImpl.java
* 版权所有(C) 2010 cuiran2001@163.com
* 创建:崔冉 Dec 2, 2010 9:49:17 AM
*/
package com.demo;
import javax.jws.WebService;
/**
* @author 崔冉
* @version 1.0.0
* @desc 实现HelloWorld接口
*/
@WebService
public class HelloWorldImpl implements HelloWorld {
/**
*
*/
public String sayHi(String text) {
return "Hi,"+text;
}
}
/**
* HelloWorldImpl.java
* 版权所有(C) 2010 cuiran2001@163.com
* 创建:崔冉 Dec 2, 2010 9:49:17 AM
*/
package com.demo;
import javax.jws.WebService;
/**
* @author 崔冉
* @version 1.0.0
* @desc 实现HelloWorld接口
*/
@WebService
public class HelloWorldImpl implements HelloWorld {
/**
*
*/
public String sayHi(String text) {
return "Hi,"+text;
}
}
然后修改web.xml里面内容
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
在WEB-INF下面创建一个beans.xml
代码如下
view plaincopy to clipboardprint?
<?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.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="helloWorld" implementor="com.demo.HelloWorldImpl" address="/HelloWorld" />
</beans>
<?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.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="helloWorld" implementor="com.demo.HelloWorldImpl" address="/HelloWorld" />
</beans>
将Web工程发布到Tomcat里面,然后启动Server
创建Client
接着创建客户端,在com.demo下面创建client-beans.xml。内容如下
view plaincopy to clipboardprint?
<?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.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
<bean id="client" class="com.demo.HelloWorld"
factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory"
class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.demo.HelloWorld" />
<property name="address" value="http://127.0.0.1:8080/cxfdemo/HelloWorld" />
</bean>
</beans>
<?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.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
<bean id="client" class="com.demo.HelloWorld"
factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory"
class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.demo.HelloWorld" />
<property name="address" value="http://127.0.0.1:8080/cxfdemo/HelloWorld" />
</bean>
</beans>
在创建测试类Client.java
view plaincopy to clipboardprint?
/**
* Client.java
* 版权所有(C) 2010 cuiran2001@163.com
* 创建:崔冉 Dec 2, 2010 9:53:30 AM
*/
package com.demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author 崔冉
* @version 1.0.0
* @desc 测试类
*/
public class Client {
public static Client self = new Client();
private HelloWorld client = null;
private Client() {
// START SNIPPET: client
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"com/demo/client-beans.xml"});
client = (HelloWorld) context.getBean("client");
}
public HelloWorld getServer() {
return client;
}
public String getText(String text) throws Exception {
String response = getServer().sayHi(text);
return response;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.println(self.getText("cuiran"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* Client.java
* 版权所有(C) 2010 cuiran2001@163.com
* 创建:崔冉 Dec 2, 2010 9:53:30 AM
*/
package com.demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author 崔冉
* @version 1.0.0
* @desc 测试类
*/
public class Client {
public static Client self = new Client();
private HelloWorld client = null;
private Client() {
// START SNIPPET: client
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"com/demo/client-beans.xml"});
client = (HelloWorld) context.getBean("client");
}
public HelloWorld getServer() {
return client;
}
public String getText(String text) throws Exception {
String response = getServer().sayHi(text);
return response;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.println(self.getText("cuiran"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
然后进行测试运行。如果遇到类似这种错误“AXB 2.1 API is being loaded from the bootstrap classloader” 说明你的web工程添加了J2EE 1.4或者是1.5 library. 这样就与jaxb-api-2.2.1.jar
冲突了,只需删除即可。
最后运行如图
源码下载地址: http://dl.dbank.com/c0vhvbrxlr
本文来自我的CSDN博客,转载请标明出处:http://blog.csdn.net/cuiran/archive/2010/12/02/6049645.aspx
分享到:
相关推荐
Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的...
使用 CXF 做 webservice 简单例子 Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻...
apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的...
弹簧-CXF-WS 一些使用spring和cxf生成webservice Rest和Soap的例子
使用Spring+CXF开发WebService Apache CXF 提供方便的Spring整合方法,可以通过注解、Spring标签式配置来暴露Web Services和消费Web Services
Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种...本资源以项目工程实例为例子,讲解如何使用CXF开发WebService服务。
基于maven的webservice例子,服务端发布和客户端调用代码,实现对mysql数据库表的curd操作
CXF例子程序,是我从网上学习,查看资料写好的程序。对于初学CXF,初始WebService很有用,我也是一个初学者。注意:需要从网上下载CXF的jar包,放到WEB-INF下的lib中,否则程序无法运行,从APACHE官方下载的apache-...
讲解了webservice的CXF框架的原理,并用具体的例子代码来教我们怎么使用。
由于联通提供WSDL版本旧(2009),尝试过Axis2,Apache CXF,Spring WS,都不能满足需求; 以 获取手机号信息的wsdl为例子 生成过程 先将tool\asix1.4\asix\WEB-INF拷贝到新建web项目的WEB-INF cd tool wsdl2java.bat ...
自己总结的一个CXF集成Spring的例子,可以直接运行,附源码! 程序的详细说明请参照博客文档 http://blog.csdn.net/sunbaojin5213344/article/details/8931600
使用CXF框架编写WebService,随书源码,简单的例子,适合初学者
文件里有CxfWebService 服务端,客户端代码,apache-cxf-2.7.6安装包和cxfWebService开发的所有jar包,还有开发过程的详细步骤,适合初学的朋友,有不足之处还望多多指教
四、CXF框架可以与spring无缝连接,就不用我们自己Endpoint了。它还能记录日志之类的 五、我们还可以使用Idea下的webservice,能够使用图形画面的方式获取本地代理和生成WSDL文件。 Activiti 介绍 ...