zoukankan      html  css  js  c++  java
  • CXF实现webService服务

    转载地址:    http://blog.csdn.net/hu_shengyang/article/details/38384597 

    以前工作中也用CXF,但都是用别人现成搭好的环境,这次自己重头搭建一遍环境。过程中也有遇到的问题,也做了简单的整理。

    对于CXF是干什么用的,我不想多说,大家都知道这是我们在java编程中webService技术的一种实现工具。我们说说为什么用CXF来实现webService:

    1.      Java的webService实现本身就是一个很耗性能的实现方案(xml与java对象之间在服务端以及客户端的互转比较消耗性能)

    2.      目前java主流的webService应用以CXF、AXIS2为主;

    3.      通过网络渠道的了解,目前CXF的效率要比AXIS2高出至少50%;

    4.      另外有一个webService的工具metro的效率比CXF高出10%;

    5.      CXF的实现资料网上可以随便找出一大堆,metro的资料相对少一些;

    6.      CXF在java应用实现中已经很成熟,企业更倾向于用这样一个成熟的解决方案;

    基于以上原因,我选择CXF来实现webService。

    参考资料:

    Java Web 服务: CXF 性能比较----CXF 与最新版本的 Axis2 和 Metro 之间的性能对比

    http://www.ibm.com/developerworks/cn/java/j-jws14/

    一   以annotation注解方式实现发布webService应用

    1、  基础环境

    新建java web工程cxf之后,下载cxf工具包。解压CXF之后,把cxf工具包lib下的jar包全部放到工程的lib下。

    此处用到的cxf工具包版本为:apache-cxf-2.7.12

    下载地址:

    http://www.apache.org/dyn/closer.cgi?path=/cxf/2.7.12/apache-cxf-2.7.12.zip

    2、  编写服务接口

    见文件HelloWorld.java

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.hsy.server;  
    2.   
    3. import java.util.List;  
    4.   
    5. import javax.jws.WebParam;  
    6. import javax.jws.WebService;  
    7.   
    8. import com.hsy.pojo.User;  
    9.   
    10. @WebService  
    11. public interface HelloWorld {  
    12.     String sayHi(@WebParam(name="text")String text);  
    13.     String sayHiToUser(User user);  
    14.     String[] SayHiToUserList(List<User> userList);  
    15. }  



    3、  服务接口实现

    见文件HelloWorldImpl.java

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.hsy.server;  
    2.   
    3. import java.util.LinkedHashMap;  
    4. import java.util.List;  
    5. import java.util.Map;  
    6.   
    7. import javax.jws.WebParam;  
    8. import javax.jws.WebService;  
    9.   
    10. import com.hsy.pojo.User;  
    11.   
    12. @WebService(endpointInterface="com.hsy.server.HelloWorld",serviceName="HelloWorld")  
    13. public class HelloWorldImpl implements HelloWorld {  
    14.     Map<Integer, User> users = new LinkedHashMap<Integer, User>();  
    15.   
    16.     public String sayHi(@WebParam(name = "text") String text) {  
    17.         return "Hello,"+text;  
    18.     }  
    19.   
    20.     public String sayHiToUser(User user) {  
    21.         users.put(users.size()+1, user);  
    22.         return "Hello,"+user.getName();  
    23.     }  
    24.   
    25.     public String[] SayHiToUserList(List<User> userList) {  
    26.         String[] result = new String[userList.size()];  
    27.         int i = 0;  
    28.         for(User u:userList){  
    29.             result[i] = "Hello " + u.getName();  
    30.             i++;  
    31.         }  
    32.         return result;  
    33.     }  
    34.   
    35.     /** 
    36.      * @param args 
    37.      */  
    38.     public static void main(String[] args) {  
    39.         // TODO Auto-generated method stub  
    40.   
    41.     }  
    42.   
    43. }  

    4、  发布服务app

    见文件webServiceApp.java

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.hsy.server;  
    2.   
    3. import javax.xml.ws.Endpoint;  
    4.   
    5. public class webServiceApp {  
    6.   
    7.     /** 
    8.      * @param args 
    9.      */  
    10.     public static void main(String[] args) {  
    11.          System.out.println("web service start");  
    12.          HelloWorldImpl implementor = new HelloWorldImpl();  
    13.          String address = "http://localhost:8080/helloWorld";  
    14.          Endpoint.publish(address, implementor);  
    15.          System.out.println("web service started");  
    16.     }  
    17.   
    18. }  



    右键 run as 选择java application发布服务;然后在浏览器输入地址:http://localhost:8080/helloWorld?wsdl

    如图:20140805132120.jpg

    说明webService服务发布成功。

    5、  客户端访问服务

    见文件HelloWorldClient.java

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.hsy.client;  
    2.   
    3. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
    4.   
    5. import com.hsy.pojo.User;  
    6. import com.hsy.server.HelloWorld;  
    7.   
    8. public class HelloWorldClient {  
    9.   
    10.     /** 
    11.      * @param args 
    12.      */  
    13.     public static void main(String[] args) {  
    14.           
    15.         //首先右键run as 运行com.hsy.server.webServiceApp类,然后再运行这段客户端代码  
    16.         JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean();  
    17.         jwpfb.setServiceClass(HelloWorld.class);  
    18.         jwpfb.setAddress("http://localhost:8080/helloWorld");  
    19.         HelloWorld hw = (HelloWorld) jwpfb.create();  
    20.         User user = new User();  
    21.         user.setName("马克思");  
    22.         user.setDescription("怀念马克思");  
    23.         System.out.println(hw.sayHiToUser(user));  
    24.           
    25.     }  
    26.   
    27. }  



    右键 run as 选择java application,控制台打印如图:

    20140805132610.jpg

    Ok,客户端访问也成功了。

    6、  附:

    User.java

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.hsy.pojo;  
    2.   
    3. import java.io.Serializable;  
    4.   
    5. @SuppressWarnings("serial")  
    6. public class User implements Serializable {  
    7.   
    8.     private String id;  
    9.     private String name;  
    10.     private String age;  
    11.     private String description;  
    12.       
    13.     public User() {  
    14.         super();  
    15.     }  
    16.   
    17.     public String getId() {  
    18.         return id;  
    19.     }  
    20.   
    21.     public void setId(String id) {  
    22.         this.id = id;  
    23.     }  
    24.   
    25.     public String getName() {  
    26.         return name;  
    27.     }  
    28.   
    29.     public void setName(String name) {  
    30.         this.name = name;  
    31.     }  
    32.   
    33.     public String getAge() {  
    34.         return age;  
    35.     }  
    36.   
    37.     public void setAge(String age) {  
    38.         this.age = age;  
    39.     }  
    40.   
    41.     public String getDescription() {  
    42.         return description;  
    43.     }  
    44.   
    45.     public void setDescription(String description) {  
    46.         this.description = description;  
    47.     }  
    48.       
    49.       
    50. }  



    二与spring集成实现webService

    1、  配置web.xml

    见文件web.xml

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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>cxf</display-name>  
    4.   <welcome-file-list>  
    5.     <welcome-file>index.html</welcome-file>  
    6.     <welcome-file>index.htm</welcome-file>  
    7.     <welcome-file>index.jsp</welcome-file>  
    8.     <welcome-file>default.html</welcome-file>  
    9.     <welcome-file>default.htm</welcome-file>  
    10.     <welcome-file>default.jsp</welcome-file>  
    11.   </welcome-file-list>  
    12.     
    13.     <context-param>  
    14.         <param-name>contextConfigLocation</param-name>  
    15.         <param-value>WEB-INF/classes/applicationContext.xml</param-value>  
    16.     </context-param>  
    17.   
    18.     <listener>  
    19.         <listener-class>  
    20.               org.springframework.web.context.ContextLoaderListener  
    21.         </listener-class>  
    22.     </listener>  
    23.   
    24.     <servlet>  
    25.         <servlet-name>CXFServlet</servlet-name>  
    26.         <servlet-class>  
    27.                org.apache.cxf.transport.servlet.CXFServlet  
    28.         </servlet-class>  
    29.         <load-on-startup>1</load-on-startup>  
    30.     </servlet>  
    31.   
    32.     <servlet-mapping>  
    33.          <servlet-name>CXFServlet</servlet-name>  
    34.          <url-pattern>/webservice/*</url-pattern>  
    35.     </servlet-mapping>  
    36.     
    37.     
    38.     
    39.     
    40.   <!-- 字符过滤器 -->    
    41.     <filter>    
    42.         <filter-name>encoding</filter-name>    
    43.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    
    44.         <init-param>    
    45.             <param-name>encoding</param-name>    
    46.             <param-value>UTF-8</param-value>    
    47.         </init-param>    
    48.         <init-param>    
    49.             <param-name>forceEncoding</param-name>    
    50.             <param-value>true</param-value>    
    51.         </init-param>    
    52.     </filter>    
    53.         
    54.         
    55.     <filter-mapping>    
    56.         <filter-name>encoding</filter-name>    
    57.         <url-pattern>*.jsp</url-pattern>    
    58.     </filter-mapping>    
    59.     <filter-mapping>    
    60.         <filter-name>encoding</filter-name>    
    61.         <url-pattern>*.html</url-pattern>    
    62.     </filter-mapping>    
    63.     <filter-mapping>    
    64.         <filter-name>encoding</filter-name>    
    65.         <url-pattern>*.do</url-pattern>    
    66.     </filter-mapping>    
    67.     <filter-mapping>    
    68.         <filter-name>encoding</filter-name>    
    69.         <url-pattern>*.action</url-pattern>    
    70.     </filter-mapping>   
    71.     <filter-mapping>    
    72.         <filter-name>encoding</filter-name>    
    73.         <url-pattern>*.jsp</url-pattern>    
    74.     </filter-mapping>    
    75.     <filter-mapping>    
    76.         <filter-name>encoding</filter-name>    
    77.         <url-pattern>*.html</url-pattern>    
    78.     </filter-mapping>    
    79.     <filter-mapping>    
    80.         <filter-name>encoding</filter-name>    
    81.         <url-pattern>*.do</url-pattern>    
    82.     </filter-mapping>    
    83.     <filter-mapping>    
    84.         <filter-name>encoding</filter-name>    
    85.         <url-pattern>*.3g</url-pattern>    
    86.     </filter-mapping>     
    87.     
    88. </web-app>  



    2、  配置applicationContext.xml

    见文件applicationContext.xml

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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   
    9.              http://cxf.apache.org/schemas/jaxws.xsd">  
    10.   
    11.       <import resource="classpath:META-INF/cxf/cxf.xml"/>  
    12.       <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>  
    13.       <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>  
    14.   
    15.       <jaxws:endpoint   
    16.              id="helloWorld"  
    17.              implementor="com.hsy.server.HelloWorldImpl"  
    18.              address="/helloWorld" />  
    19.   
    20.      <bean id="client"   
    21.             class="com.hsy.server.HelloWorld"   
    22.             factory-bean="clientFactory"   
    23.             factory-method="create"/>  
    24.   
    25.      <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  
    26.             <property name="serviceClass" value="com.hsy.server.HelloWorld"/>  
    27.             <property name="address" value="http://localhost:8080/cxf/webservice/helloWorld"/>  
    28.      </bean>  
    29.        
    30. </beans>  



    3、  修改客户端代码

    见文件HelloWorldClient.java

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.hsy.client;  
    2.   
    3. import java.util.ArrayList;  
    4. import java.util.List;  
    5.   
    6. import org.springframework.beans.factory.BeanFactory;  
    7. import org.springframework.beans.factory.xml.XmlBeanFactory;  
    8. import org.springframework.context.ApplicationContext;  
    9. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    10. import org.springframework.core.io.FileSystemResource;  
    11. import org.springframework.core.io.Resource;  
    12.   
    13. import com.hsy.pojo.User;  
    14. import com.hsy.server.HelloWorld;  
    15.   
    16. public class HelloWorldClient {  
    17.   
    18.     /** 
    19.      * @param args 
    20.      */  
    21.     public static void main(String[] args) {  
    22.           
    23.         //Resource resource= new FileSystemResource("F:/workspaces4me2013/.metadata/.me_tcat/WEB-INF/classes/applicationContext.xml");     
    24.         //BeanFactory factory= new XmlBeanFactory(resource );   
    25.         ApplicationContext factory = new ClassPathXmlApplicationContext("/applicationContext.xml");  
    26.         HelloWorld client = (HelloWorld)factory.getBean("client");  
    27.         User user1 = new User();  
    28.         user1.setName("马克思");  
    29.         user1.setDescription("怀念马克思");  
    30.         User user2 = new User();  
    31.         user2.setName("恩格斯");  
    32.         user2.setDescription("怀念恩格斯");  
    33.         List<User> userList= new ArrayList<User>();  
    34.         userList.add(user1);  
    35.         userList.add(user2);  
    36.         String[] res = client.SayHiToUserList(userList);  
    37.         System.out.println(res[0]);  
    38.         System.out.println(res[1]);    
    39.           
    40.     }  
    41.   
    42. }  



    4、  启动tamcat发布webService

    然后在浏览器输入地址:http://localhost:8080/cxf/webservice/helloWorld?wsdl

    如图:20140805133642.jpg

    说明webService服务发布成功。

    5、  运行客户端代码访问webService

    右键 run as 选择java application,控制台打印如图:

    20140805134838.jpg

    Ok,客户端访问也成功了。

    -------------------------------------------

    关于   applicationContext.xml 中的引用一下三种xml文件。 若cxf版本低于3.0的,则要全部引用。若高于3.0版本的,则只需要引用第一个cxf.xml文件就可以了。 

    1.  <import resource="classpath:META-INF/cxf/cxf.xml"/>  
    2.       <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>  
    3.       <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>  

    ---------------------------------------------

    远程调用客户端,生成客户端代码,有两种方式,一个是通过wsdl的后缀名的wsdl文件,两外一个通过url来构建,两种方法,均可在myeclipse中,创建web webservice Client 工程下,进行构建生成本地客户端代码。 

    生成客户端代码之后,关于如何调用客户端那?   则仔细查看生成的客户端代码中,有一个class文件,以 XXXService.java结尾的,该class就是该客户端代码的工厂类,调用该类中的getHelloWorldImplPort() 方法,进行创建工厂实例。

          如:    

                   HelloWorld_Service factory=new HelloWorld_Service(); // 充当该客户端的工厂类。创建实例。 
                   HelloWorld hello=factory.getHelloWorldImplPort(); //   创建实例 hello
                   System.out.println(hello.sayHi("lhy----0000")); //  调用 helloWorld类下的sayHi(String name) 方法。

                

  • 相关阅读:
    小程序 图片和文字放在一行对齐的方法
    Linux下Redis安装使用教程
    关系型数据库和非关系型数据库的区别
    微信小程序scroll-view 横向和纵向scroll-view组件
    ThinkPHP5.0手把手实现手机阿里云短信验证
    极验(Geetest) Laravel 5 集成开发包,让验证更安全
    (进阶篇)PHP(thinkphp5框架)实现用户注册后邮箱验证,激活帐号
    详解PhpSpreadsheet设置单元格
    使用PhpSpreadsheet将Excel导入到MySQL数据库
    【JZOJ4783】【NOIP2016提高A组模拟9.15】Osu
  • 原文地址:https://www.cnblogs.com/yongwuqing/p/5719182.html
Copyright © 2011-2022 走看看