Eclipse+CXF框架开发Web服务实战
来源:互联网 发布:网络歌手冬冬 编辑:程序博客网 时间:2024/06/02 05:29
文章参考:http://wenku.baidu.com/view/3fbb558602d276a200292e8a.html
一、 说明
采用CXF框架开发webservice。
所用软件及版本如下。
操作系统:Window XP SP3。
JDK:JDK1.6.0_07,http://www.oracle.com/technetwork/java/javase/downloads/index.html。
Tomcat:apache-tomcat-6.0.14.exe,http://tomcat.apache.org/。
IDE:eclipse-jee-juno-SR1-win32.zip,http://www.eclipse.org/downloads/。
CXF:apache-cxf-2.6.10.zip,http://cxf.apache.org/download.html。
二、 JDK配置
安装JDK1.6.0,配置环境变量。
JAVA_HOME:C:\Program Files\Java\jre1.6.0_07。
Path:C:\Program Files\Java\jre1.6.0_07\bin。
CLASSPATH:C:\Program Files\Java\jre1.6.0_07\lib。
因本地只安装了JRE,故配置信息按JRE目录来设置。
三、 Tomcat配置
安装Tomcat,运行apache-tomcat-6.0.14.exe。
端口号:8080
用户名:admin
密码:111111
安装完成后,启动tomcat。
访问:http://localhost:8080/。出现如下界面则部署成功。
四、 CXF服务端
1、下载apache-cxf-2.6.10.zip包,解压后数据如下。
2、新建Dynamic Web Project。
File—New—Project。
工程名:MyService。
Target runtime要与安装的Tomcat一致,若不一致点击“New Runtime”设置。
比如我本地安装的Tomcat为v6.0。
Tomcat installation directory选择Tomcat安装目录。
Default output folder设置为:WebContent/WEB-INF/classes。
3、导入CXF库文件。
CXF库配置。
选中apache-cxf-2.6.10包下的lib目录,导入所有的jar文件。
本机目录为F: \Java开发\apache-cxf-2.6.10\lib。
添加完成后,会出现CXF名称库目录。
4、创建接口IcxfWB。
工程右键—New—Interface,添加代码:
package com.yxj;import javax.jws.WebService;import javax.jws.WebParam;import javax.jws.WebMethod;@WebServicepublic interface IcxfWB {@WebMethodString sayHello(@WebParam(name="name") String name);}说明:
@WebService:标记表示该接口是一个WebService服务。
@WebMethod:标记表示WebService中的方法。
@WebParam(name="paramName")表示方法中的参数,name属性限制了参数的名称,若没有指定该属性,参数将会被重命名。
5、创建服务实现类CxfWBImpl。
package com.yxj;public class CxfWBImpl implements IcxfWB {public String sayHello(String name) {return "Hello "+name;}}6、编辑WebContent/WEB-INF下web.xml文件。
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>MyService</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/service-beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list></web-app>7、在WebContent/WEB-INF下创建刚才指定的service-beans.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" 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"/> <bean id="SayHello" class="com.yxj.CxfWBImpl" /> <jaxws:endpoint id="sayHello" implementor="#SayHello" address="/sayHello"/></beans>五、 WebService部署
1、 打包工程
右键工程—Export—WAR file,指定输出路径。
2、 将war文件移动至apache-tomcat\webapps下。
本地操作是,复制F:\MyService.war到目录D:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps。
3、 复制CFX文件到tomcat下。
复制F:\常用软件\Java开发\apache-cxf-2.6.10\lib目录下文件到
F:\JavaRoot\workspace\MyService\WebContent\WEB-INF\lib。
4、 重启Tomcat。
5、 浏览器访问http://localhost:8080/MyService。
六、 CXF客户端
1、生成Stub
CXF提供了一个名为“wsdl2java.bat”的工具,该工具可以通过WSDL为特定的服务创建 stub。本地目录,F:\常用软件\Java开发\apache-cxf-2.6.10\bin\下wadl2java工具。
命令wsdl2java –p 生成代码的包名 –d 生成代码的路径 wsdl地址。
本地命令如下。
wsdl2java -p com.yxj.client -d F:\clientsrc http://localhost:8080/MyService/sayHello?wsdl
生成结果文件在F:\clientsrc,生成文件如图。
2、新建Java Project。
3、将第一步中生成的F:\clientsrc文件夹下的所有java文件移至工程src下,刷新工程。
4、添加CXF包。
使用创建Service时配置好的CXF库。
6、 新建包含有main方法的Class,代码如下。
package com.yxj.client;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.yxj.client.IcxfWB;public class ClientCall {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"com/yxj/client/client-beans.xml"}); IcxfWB client = (IcxfWB)context.getBean("sayHello2"); String response = client.sayHello("World"); System.out.println("Response: " + response); System.exit(0);}}7、 在类ClientCall同级目录,即com/yxj/client下新建client-beans.xml配置文件。
文件内容如下。
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <jaxws:client id="sayHello2" serviceClass="com.yxj.client.IcxfWB" address="http://localhost:8080/MyService/sayHello?wsdl"/> </beans>该方式访问ws服务是利用spring的依赖注入法,其中id是spring IOC容器唯一标识符,在代码中也是通过id获得服务对象的(context.getBean("sayHello2"));serviceClass是Webservices服务接口;address是服务的wsdl地址。
8、 运行客户端,控制台输出。
Hello World
OK,至此大功告成!
- Eclipse+CXF框架开发Web服务实战
- Eclipse+CXF框架开发Web服务实战
- Eclipse+CXF框架开发Web服务实战
- Eclipse+CXF框架开发Web服务实战
- WEB服务框架--CXF简介
- Eclipse + Jboss + CXF构建Web Service服务
- 使用CXF开发web service服务中报错
- Web Service ---- CXF + SPRING 开发 SOAP服务
- SpringBoot + CXF 开发Web Service 服务
- 利用Apache CXF 框架简单发布Web服务
- C++ 开发 Web 服务框架
- C++ 开发 Web 服务框架
- cxf框架Demo2(web项目中创建类的cxf服务)
- cxf框架Demo3(在web项目中创建接口的cxf服务)
- 使用 Apache CXF 和 Aegis 开发 Web 服务
- Eclipse安装CXF插件开发java web service 集成Spring
- Eclipse安装CXF插件开发java web service 集成Spring
- Eclipse安装CXF插件开发java web service 集成Spring
- 设置jframe的location位置
- Apache 解决500 Internal Server Error 问题
- cout/cerr/clog之异同极其重定向
- _beginthreadex参数说明
- 使用TinyXml库需要注意的几个问题
- Eclipse+CXF框架开发Web服务实战
- 让Editplus和SVN集成
- Spring Security3 页面 权限标签
- 理解RESTful架构
- [数学函数]sqrt()—开方函数(取平方根值)
- SMOD和CMOD
- Linux中Apache+Tomcat+JK实现负载均衡和群集
- windows批处理(cmd/bat)编程详解
- 高仿打飞机代码实现