zoukankan      html  css  js  c++  java
  • Eclipse+CXF框架开发Web服务实战

    一、 说明 采用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 FilesJavajre1.6.0_07。

    Path:C:Program FilesJavajre1.6.0_07in。

    CLASSPATH:C:Program FilesJavajre1.6.0_07lib。

    因本地只安装了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.10lib。

    添加完成后,会出现CXF名称库目录。

    4、创建接口IcxfWB。 工程右键—New—Interface,添加代码:

    1. package com.yxj;  
    2.   
    3. import javax.jws.WebService;  
    4. import javax.jws.WebParam;  
    5. import javax.jws.WebMethod;  
    6.   
    7. @WebService  
    8. public interface IcxfWB {  
    9.     @WebMethod  
    10.     String sayHello(@WebParam(name="name") String name);  
    11. }  

    说明:
    @WebService:标记表示该接口是一个WebService服务。
    @WebMethod:标记表示WebService中的方法。
    @WebParam(name="paramName")表示方法中的参数,name属性限制了参数的名称,若没有指定该属性,参数将会被重命名。

    5、创建服务实现类CxfWBImpl。

    1. package com.yxj;  
    2.   
    3. public class CxfWBImpl implements IcxfWB {  
    4.     public String sayHello(String name) {  
    5.         return "Hello "+name;  
    6.     }  
    7. }  

    6、编辑WebContent/WEB-INF下web.xml文件。

    [plain] view plaincopy
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <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">  
    3.   <display-name>MyService</display-name>  
    4.   <context-param>  
    5.     <param-name>contextConfigLocation</param-name>  
    6.     <param-value>WEB-INF/service-beans.xml</param-value>  
    7.   </context-param>  
    8.   <listener>  
    9.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    10.   </listener>  
    11.   <servlet>  
    12.    <servlet-name>CXFServlet</servlet-name>  
    13.    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
    14.   </servlet>  
    15.   <servlet-mapping>  
    16.     <servlet-name>CXFServlet</servlet-name>  
    17.     <url-pattern>/*</url-pattern>  
    18.   </servlet-mapping>  
    19.   <welcome-file-list>  
    20.     <welcome-file>index.html</welcome-file>  
    21.     <welcome-file>index.htm</welcome-file>  
    22.     <welcome-file>index.jsp</welcome-file>  
    23.     <welcome-file>default.html</welcome-file>  
    24.     <welcome-file>default.htm</welcome-file>  
    25.     <welcome-file>default.jsp</welcome-file>  
    26.   </welcome-file-list>  
    27. </web-app>  

    7、在WebContent/WEB-INF下创建刚才指定的service-beans.xml文件。

    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.   xmlns:jaxws="http://cxf.apache.org/jaxws"  
    5.   xsi:schemaLocation="  
    6.     http://www.springframework.org/schema/beans  
    7.     http://www.springframework.org/schema/beans/spring-beans.xsd  
    8.     http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
    9.   <import resource="classpath:META-INF/cxf/cxf.xml"/>  
    10.   <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>  
    11.   <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>  
    12.   <bean id="SayHello" class="com.yxj.CxfWBImpl" />  
    13.   <jaxws:endpoint id="sayHello" implementor="#SayHello" address="/sayHello"/>  
    14. </beans>  

    五、 WebService部署
    1、 打包工程
    右键工程—Export—WAR file,指定输出路径。

    2、 将war文件移动至apache-tomcatwebapps下。 本地操作是,复制F:MyService.war到目录D:Program FilesApache Software FoundationTomcat 6.0webapps。
    3、 复制CFX文件到tomcat下。 复制F:常用软件Java开发apache-cxf-2.6.10lib目录下文件到 F:JavaRootworkspaceMyServiceWebContentWEB-INFlib。
    4、 重启Tomcat。 5、 浏览器访问http://localhost:8080/MyService。

    六、 CXF客户端

    1、生成Stub CXF提供了一个名为“wsdl2java.bat”的工具,该工具可以通过WSDL为特定的服务创建 stub。

    本地目录,F:常用软件Java开发apache-cxf-2.6.10in下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,代码如下。

    1. package com.yxj.client;  
    2.   
    3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    4. import com.yxj.client.IcxfWB;  
    5.   
    6. public class ClientCall {  
    7.   
    8.     /** 
    9.      * @param args 
    10.      */  
    11.     public static void main(String[] args) {  
    12.         // TODO Auto-generated method stub  
    13.         ClassPathXmlApplicationContext context   
    14.         = new ClassPathXmlApplicationContext(new String[] {"com/yxj/client/client-beans.xml"});   
    15.   
    16.         IcxfWB client = (IcxfWB)context.getBean("sayHello2");   
    17.         String response = client.sayHello("World");   
    18.         System.out.println("Response: " + response);   
    19.         System.exit(0);  
    20.     }  
    21. }  

    7、 在类ClientCall同级目录,即com/yxj/client下新建client-beans.xml配置文件。
    文件内容如下。

    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"   
    3.        xmlns:jaxws="http://cxf.apache.org/jaxws"   
    4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    5.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
    6.           http://www.springframework.org/schema/beans/spring-beans.xsd   
    7.           http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
    8.     <jaxws:client id="sayHello2" serviceClass="com.yxj.client.IcxfWB" address="http://localhost:8080/MyService/sayHello?wsdl"/>   
    9. </beans>  

    该方式访问ws服务是利用spring的依赖注入法,其中id是spring IOC容器唯一标识符,在代码中也是通过id获得服务对象的(context.getBean("sayHello2"));serviceClass是Webservices服务接口;address是服务的wsdl地址。

    8、 运行客户端,控制台输出。

    [plain] view plaincopy
    1. Hello World  

    OK,至此大功告成!

  • 相关阅读:
    oralce索引的使用
    oracle中connect by prior的使用
    oracle函数listagg使用
    oracle函数的使用
    redis高可用集群搭建
    Node.js安装及环境配置之Windows篇
    Repeater 合并单元格
    c#16进制转浮点数单精度类型
    EF Core 实体映射表或视图
    docker 构建filebeat镜像
  • 原文地址:https://www.cnblogs.com/tbyang/p/3939352.html
Copyright © 2011-2022 走看看