CXF实现webService服务(一)

前端之家收集整理的这篇文章主要介绍了CXF实现webService服务(一)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前工作中也用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
  1. package com.hsy.server;  
  2.   
  3. import java.util.List;  
  4. import javax.jws.WebParam;  
  5. import javax.jws.WebService;  
  6.   
  7. import com.hsy.pojo.User;  
  8. @WebService  
  9. public interface HelloWorld {  
  10.     String sayHi(@WebParam(name="text")String text);  
  11.     String sayHiToUser(User user);  
  12.     String[] SayHiToUserList(List<User> userList);  
  13. }  


 

3、  服务接口实现

文件HelloWorldImpl.java

copy
    import java.util.LinkedHashMap;  
  1. import java.util.List;  
  2. import java.util.Map;  
  3. @WebService(endpointInterface="com.hsy.server.HelloWorld",serviceName="HelloWorld")  
  4. class HelloWorldImpl implements HelloWorld {  
  5.     Map<Integer, User> users = new LinkedHashMap<Integer, User>();  
  6.     public String sayHi(@WebParam(name = "text") String text) {  
  7.         return "Hello,"+text;  
  8.     }  
  9. public String sayHiToUser(User user) {  
  10.         users.put(users.size()+1, user);  
  11.         +user.getName();  
  12.     }  
  13.     public String[] SayHiToUserList(List<User> userList) {  
  14.         String[] result = new String[userList.size()];  
  15. int i = 0;  
  16. for(User u:userList){  
  17.             result[i] = "Hello " + u.getName();  
  18.             i++;  
  19.         }  
  20. return result;  
  21.     /** 
  22.      * @param args 
  23.      */  
  24. static void main(String[] args) {  
  25.         // TODO Auto-generated method stub  
  26. }  

4、  发布服务app

文件webServiceApp.java

copy
    import javax.xml.ws.Endpoint;  
  1. class webServiceApp {  
  2.          System.out.println("web service start");  
  3.          HelloWorldImpl implementor = new HelloWorldImpl();  
  4.          String address = "http://localhost:8080/helloWorld";  
  5.          Endpoint.publish(address, implementor);  
  6.          System.out.println("web service started");  
  7. }  



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

如图:20140805132120.jpg


说明webService服务发布成功。

 

5、  客户端访问服务

文件HelloWorldClient.java

copy
    package com.hsy.client;  
  1. import org.apache.cxf.jaxws.JaxWsProxyfactorybean;  
  2. import com.hsy.pojo.User;  
  3. import com.hsy.server.HelloWorld;  
  4. class HelloWorldClient {  
  5.     /** 
  6.      * @param args 
  7.      */  
  8. void main(String[] args) {  
  9.           
  10. //首先右键run as 运行com.hsy.server.webServiceApp类,然后再运行这段客户端代码  
  11.         JaxWsProxyfactorybean jwpfb = new JaxWsProxyfactorybean();  
  12.         jwpfb.setServiceClass(HelloWorld.class);  
  13.         jwpfb.setAddress("http://localhost:8080/helloWorld");  
  14.         HelloWorld hw = (HelloWorld) jwpfb.create();  
  15.         User user = new User();  
  16.         user.setName("马克思");  
  17.         user.setDescription("怀念马克思");  
  18.         System.out.println(hw.sayHiToUser(user));  
  19. }  


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

20140805132610.jpg


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

6、  附:

User.java

copy
    package com.hsy.pojo;  
  1. import java.io.Serializable;  
  2. @SuppressWarnings("serial")  
  3. class User implements Serializable {  
  4. private String id;  
  5. private String name;  
  6. private String age;  
  7. private String description;  
  8.       
  9. public User() {  
  10. super();  
  11. public String getId() {  
  12. return id;  
  13. void setId(String id) {  
  14. this.id = id;  
  15. public String getName() {  
  16. return name;  
  17. void setName(String name) {  
  18. this.name = name;  
  19. public String getAge() {  
  20. return age;  
  21. void setAge(String age) {  
  22. this.age = age;  
  23. public String getDescription() {  
  24. return description;  
  25. void setDescription(String description) {  
  26. this.description = description;  
  27.       
  28. }  


 

二与spring集成实现webService

1、  配置web.xml

文件web.xml

[html]  copy
    <?xml version="1.0" encoding="UTF-8"?>  
  1. <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">  
  2.   display-name>cxf</>  
  3.   welcome-file-list     welcome-file>index.html     >index.htm>index.jsp>default.html>default.htm>default.jsp     
  4. context-param         param-name>contextConfigLocation         param-value>WEB-INF/classes/applicationContext.xmllistenerlistener-class               org.springframework.web.context.ContextLoaderListener  
  5. servletservlet-name>CXFServletservlet-class                org.apache.cxf.transport.servlet.CXFServlet  
  6. load-on-startup>1servlet-mapping                    url-pattern>/webservice/*     
  7.   <!-- 字符过滤器 -->    
  8. filter>    
  9. filter-name>encoding>    
  10. filter-class>org.springframework.web.filter.CharacterEncodingFilterinit-param                          >UTF-8>forceEncoding>true         
  11.         
  12. filter-mapping>*.jsp>*.html>*.do>*.action>   
  13. >*.3g>     
  14. web-app>  


 

2、  配置applicationContext.xml

文件applicationContext.xml

copy
    beans xmlns="http://www.springframework.org/schema/beans"  
  1.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.        xmlns:jaxws="http://cxf.apache.org/jaxws"  
  3.        xsi:schemaLocation="  
  4.              http://www.springframework.org/schema/beans  
  5.              http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.              http://cxf.apache.org/jaxws   
  7.              http://cxf.apache.org/schemas/jaxws.xsd"       import resource="classpath:Meta-INF/cxf/cxf.xml"/>  
  8.       import resource="classpath:Meta-INF/cxf/cxf-extension-soap.xml"/>  
  9. import resource="classpath:Meta-INF/cxf/cxf-servlet.xml"jaxws:endpoint   
  10.              id="helloWorld"  
  11.              implementor="com.hsy.server.HelloWorldImpl"  
  12.              address="/helloWorld"       bean id="client"   
  13.             class="com.hsy.server.HelloWorld"   
  14.             factory-bean="clientFactory"   
  15.             factory-method="create"      bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyfactorybean"property name="serviceClass" value="com.hsy.server.HelloWorld"property name="address" value="http://localhost:8080/cxf/webservice/helloWorld"bean        
  16. beans>  


 

3、  修改客户端代码

文件copy

    import java.util.ArrayList;  
  1. import org.springframework.beans.factory.beanfactory;  
  2. import org.springframework.beans.factory.xml.Xmlbeanfactory;  
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. import org.springframework.core.io.FileSystemResource;  
  6. import org.springframework.core.io.Resource;  
  7. //Resource resource= new FileSystemResource("F:/workspaces4me2013/.Metadata/.me_tcat/WEB-INF/classes/applicationContext.xml");     
  8.         //beanfactory factory= new Xmlbeanfactory(resource );   
  9.         ApplicationContext factory = new ClassPathXmlApplicationContext("/applicationContext.xml");  
  10.         HelloWorld client = (HelloWorld)factory.getBean("client");  
  11.         User user1 = new User();  
  12.         user1.setName("马克思");  
  13.         user1.setDescription("怀念马克思");  
  14.         User user2 =          user2.setName("恩格斯");  
  15.         user2.setDescription("怀念恩格斯");  
  16.         List<User> userList= new ArrayList<User>();  
  17.         userList.add(user1);  
  18.         userList.add(user2);  
  19.         String[] res = client.SayHiToUserList(userList);  
  20.         System.out.println(res[0]);  
  21.         System.out.println(res[1]);    
  22.           
  23. }  


 

4、  启动tamcat发布webService

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

如图:20140805133642.jpg


说明webService服务发布成功。

 

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

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

20140805134838.jpg


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

 此篇实现了webService服务的发布以及在本工程下的客户端调用服务的示例,或许不是很直观。

请看下一篇CXF客户端代码生成与服务调用(二)

http://blog.csdn.net/hu_shengyang/article/details/38384839

本文参照了:使用 CXF webservice简单例子

http://www.cnblogs.com/frankliiu-java/articles/1641949.html

猜你在找的WebService相关文章