xfire+spring+maven构建webservice服务器和客户端

简介:

本文转自:http://jishiweili.iteye.com/blog/2087930 

文章主要介绍:

1:用xfire+spring+maven构建webservice服务器。

2:用xfire的eclipse插件生成客户端访问方式。

3:以知接口和bean生成客户端。

4:客户端动态访问。包括返回java自定义对象。

博客代码奉上:http://pan.baidu.com/s/1dFr4EfZ 

1:用xfire+spring+maven构建webservice服务器。

(一)肯定首先配置spring上下文监听器和xfire的servert配置,将下列代码加入web.xml中:

Xml代码  收藏代码

  1. <?xml version="1.0" encoding="UTF-8"?>    

  2.     

  3. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    

  4.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    

  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    

  6.     id="WebApp_ID" version="2.5">    

  7.     <display-name>webservice</display-name>    

  8.     <!-- springmvc 上下文监听器 ContextLoaderListener -->  

  9.     <listener>  

  10.           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  

  11.     </listener>  

  12.     <context-param>  

  13.         <param-name>contextConfigLocation</param-name>  

  14.         <param-value>classpath:spring/*.xml</param-value>  

  15.     </context-param>  

  16.     <!-- springmvc 上下文监听器  ContextLoaderListener-->  

  17.      <servlet>    

  18.       <servlet-name>xfire</servlet-name>    

  19.       <servlet-class>    

  20.        org.codehaus.xfire.spring.XFireSpringServlet    

  21.       </servlet-class>    

  22.      </servlet>    

  23.      <servlet-mapping>    

  24.       <servlet-name>xfire</servlet-name>    

  25.       <url-pattern>/service/*</url-pattern>    

  26.      </servlet-mapping>    

  27. </web-app>  

 (二)配置 spring的配置文件,最基本的包括导入xfire.xml和baseWebService,剩余2个bean是自定义的webservice接口和实现类。代码如下:

Xml代码  收藏代码

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  

  3. <beans>  

  4.         <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />     

  5.          <bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true">      

  6.           <property name="serviceFactory" ref="xfire.serviceFactory" />      

  7.           <property name="xfire" ref="xfire" />     

  8.          </bean>     

  9.          <bean id="bookWS" class="com.xiaoji.webservice.xfire.service.impl.BookServiceImpl">    

  10.          </bean>     

  11.          <bean id="bookService" parent="baseWebService">      

  12.           <property name="serviceBean" ref="bookWS" />      

  13.           <property name="serviceClass" value="com.xiaoji.webservice.xfire.service.BookService" />     

  14.          </bean>   

  15. </beans>    

 (三)剩下的就是自定义接口和实现类,对象bean.代码如下

book.java

Java代码  收藏代码

  1. package com.xiaoji.webservice.xfire.entity;  

  2.   

  3. public class Book {  

  4.   

  5.     private int bookId;  

  6.     private String name;  

  7.       

  8.     public Book(){  

  9.           

  10.     }  

  11.       

  12.     public Book(int bookId,String name){  

  13.         this.bookId = bookId;  

  14.         this.name = name;  

  15.     }  

  16.   

  17.     public int getBookId() {  

  18.         return bookId;  

  19.     }  

  20.   

  21.     public void setBookId(int bookId) {  

  22.         this.bookId = bookId;  

  23.     }  

  24.   

  25.     public String getName() {  

  26.         return name;  

  27.     }  

  28.   

  29.     public void setName(String name) {  

  30.         this.name = name;  

  31.     }  

  32.       

  33.       

  34. }  

 BookService和BookServiceimpl,定义了2个方法,一个传入字符串并返回字符串,另外一个传入个数字返回个对象:

Java代码  收藏代码

  1. package com.xiaoji.webservice.xfire.service;  

  2.   

  3.   

  4. import com.xiaoji.webservice.xfire.entity.Book;  

  5.   

  6. public interface BookService {  

  7.   

  8.     public Book getBookById(int id);  

  9.     public String sayHello(String str);  

  10. }  

 

Java代码  收藏代码

  1. package com.xiaoji.webservice.xfire.service.impl;  

  2.   

  3. import java.util.ArrayList;  

  4. import java.util.List;  

  5.   

  6. import com.xiaoji.webservice.xfire.entity.Book;  

  7. import com.xiaoji.webservice.xfire.service.BookService;  

  8.   

  9. public class BookServiceImpl implements BookService {  

  10.   

  11.     public Book getBookById(int id) {  

  12.         Book book = new Book(1,"china");  

  13.         return book;  

  14.     }  

  15.   

  16.     public String sayHello(String str) {  

  17.         // TODO Auto-generated method stub  

  18.         return "this is " + str + ".";  

  19.     }  

  20.   

  21.   

  22. }  

 最后还是要贴上pom.xml源码

Java代码  收藏代码

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  

  3.   <modelVersion>4.0.0</modelVersion>  

  4.   <groupId>com.xiaoji.webservice</groupId>  

  5.   <artifactId>webservice-xfire</artifactId>  

  6.   <packaging>war</packaging>  

  7.   <version>0.0.1-SNAPSHOT</version>  

  8.   <name>webservice-xfire Maven Webapp</name>  

  9.   <url>http://maven.apache.org</url>  

  10.   <dependencies>  

  11.     <dependency>  

  12.       <groupId>junit</groupId>  

  13.       <artifactId>junit</artifactId>  

  14.       <version>3.8.1</version>  

  15.       <scope>test</scope>  

  16.     </dependency>  

  17.     <dependency>  

  18.     <groupId>org.codehaus.xfire</groupId>  

  19.     <artifactId>xfire-core</artifactId>  

  20.     <version>1.2.5</version>  

  21.     </dependency>   

  22.     <dependency>  

  23.         <groupId>org.codehaus.xfire</groupId>  

  24.         <artifactId>xfire-spring</artifactId>  

  25.         <version>1.2.6</version>  

  26.     </dependency>   

  27.     <dependency>    

  28.       <groupId>org.springframework</groupId>    

  29.       <artifactId>spring-web</artifactId>    

  30.       <version>3.2.8.RELEASE</version>    

  31.     </dependency>    

  32.     </dependencies>    

  33.   <build>  

  34.     <finalName>webservice-xfire</finalName>  

  35.   </build>  

  36. </project>  

1.服务器注解:服务器配置基本这样,有一点不理解的地方就是不能返回List,map等集合对象。如果有人实现可以互相交流哈。

启动jetty,访问127.0.0.1:8080/webservice-xfire/service/BookService?wsdl。

启动tomcat7,访问:127.0.0.1:8888/service/BookService?wsdl。

2.用xfire的eclipse插件生成客户端访问方式。

如果你eclipse上没有xfire插件,请参考我的博客:http://jishiweili.iteye.com/admin/blogs/2086145,进行xfire插件安装,

新建个maven-archetype-webapp,右键新建的项目,选择new-》other-》Code generation from WSDL document,wsdl url or path为:127.0.0.1:8080/webservice-xfire/service/BookService?wsdl;output directory选择新建项目下的src/java/main;最后输入package(随便输);确定即可。

你会发现包里面多了个xfire组包 可以删掉,新建个客户端类,代码如下:

Java代码  收藏代码

  1. package com.xiaoji.webservice.xfire.client;  

  2.   

  3. import java.net.MalformedURLException;  

  4.   

  5.   

  6.   

  7. public class WebserviceXfireClient {  

  8.   

  9.     public static void main(String[] args) throws Exception {  

  10.         //xfire客户端访问webservice第3种方式  

  11.         //xfire自动生成客户端  

  12.         BookServiceClient client = new BookServiceClient();  

  13.         BookServicePortType bs = client.getBookServiceHttpPort();  

  14.         System.out.println(bs.sayHello("小吉"));  

  15.         System.out.println(bs.getBookById(0).getName().getValue());  

  16.     }  

  17.       

  18. }  

 

最后贴上xfire的pom.xml:

Java代码  收藏代码

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  

  3.   <modelVersion>4.0.0</modelVersion>  

  4.   <groupId>com.xiaoji.webservice.xfire</groupId>  

  5.   <artifactId>eclipseplugin</artifactId>  

  6.   <packaging>war</packaging>  

  7.   <version>0.0.1-SNAPSHOT</version>  

  8.   <name>eclipseplugin Maven Webapp</name>  

  9.   <url>http://maven.apache.org</url>  

  10.   <dependencies>  

  11.       <dependency>  

  12.     <groupId>org.codehaus.xfire</groupId>  

  13.     <artifactId>xfire-all</artifactId>  

  14.     <version>1.2.6</version>  

  15. </dependency>  

  16.   </dependencies>  

  17.   <build>  

  18.     <finalName>eclipseplugin</finalName>  

  19.   </build>  

  20. </project>  

3:第二种访问方式是知道接口的自定义对象bean时用,这里要注意,必须类名和包名一直,要不访问不到的。

这里只贴客户端代码。pom.xml,类和接口如上,

Java代码  收藏代码

  1. // 第一种访问方式需要知道接口和bean  

  2. public static void testClient1() throws MalformedURLException {  

  3.     String serviceUrl = "http://127.0.0.1:8888/service/BookService";  

  4.   

  5.     XFire xfire = XFireFactory.newInstance().getXFire();  

  6.   

  7.     XFireProxyFactory factory = new XFireProxyFactory(xfire);  

  8.   

  9.     Service service = new ObjectServiceFactory().create(BookService.class);  

  10.   

  11.     BookService bs = (BookService) factory.create(service, serviceUrl);  

  12.   

  13.     System.out.println(bs.sayHello("小吉111111"));  

  14.   

  15.     System.out.println(bs.getBookById(0).getName());  

  16. }  

 4:第4中客户端动态访问,这里我刚刚开始的时候始终不能返回自定义对象,返回的类型为DocumentImpl,我就想办法解析这个类型查找类型相关资料,终于解析成功,这里共享给大家:

Java代码  收藏代码

  1. // 动态客户端第2种访问方式  

  2.     public static void testClient2() throws MalformedURLException, Exception {  

  3.         Client client = new Client(  

  4.                 new URL(  

  5.                         "http://127.0.0.1:8888/service/BookService?wsdl"));  

  6.         Object[] results = client.invoke("sayHello"new Object[] { "吉凌夷" });  

  7.         Object[] results2 = client.invoke("getBookById"new Object[] { 1 });  

  8.         System.out.println(results[0]);  

  9.         //返回对象解析  

  10.         Document xmlTree = (Document)results2[0];  

  11.         Element root = xmlTree.getDocumentElement();  

  12.         parseElement(root);  

  13.         System.out.println(map.get("bookId")+"->"+map.get("name"));  

  14.     }  

  15.   

  16.     /** 

  17.      * 解析返回树 

  18.      * @param root 

  19.      */  

  20.     private static void parseElement(Element root) {  

  21.         String key = root.getNodeName();  

  22.         NodeList list = root.getChildNodes();  

  23.         for (int i = 0; i < list.getLength(); i++) {  

  24.             Node node = list.item(i);  

  25.             if (node instanceof Element) {  

  26.                 Element e = (Element) node;  

  27.                 parseElement(e);  

  28.             } else if (node instanceof Text) {  

  29.                 Text t = (Text) node;  

  30.                 map.put(key, t.getNodeValue());  

  31.             }  

  32.         }  

  33.     }  

 注:pom.xml和第2中是一样的,这里要说一下xfire,官网很久没更新,官网上说的是和cxf合并了,具体咋样大家有待考证。

剩下的就是axis2,也是资料最多的。如果不是很忙的话我会很快更新出来。

下面我会上传源码:其中一个是服务器,2个是客户端,第2中访问和第3中访问我放在一起,请自行分别。




本文转自 兴趣e族 51CTO博客,原文链接:http://blog.51cto.com/simplelife/1847131
相关文章
|
6天前
|
Java API 微服务
【Spring Boot系列】通过OpenAPI规范构建微服务服务接口
【4月更文挑战第5天】通过OpenAPI接口构建Spring Boot服务RestAPI接口
|
2月前
|
消息中间件 运维 网络协议
客户端和服务器之间的通信
客户端和服务器之间的通信
32 0
|
2月前
|
监控 关系型数据库 Linux
|
19天前
|
网络协议 Python
pythonTCP客户端编程连接服务器
【4月更文挑战第6天】本教程介绍了TCP客户端如何连接服务器,包括指定服务器IP和端口、发送连接请求、处理异常、进行数据传输及关闭连接。在Python中,使用`socket`模块创建Socket对象,然后通过`connect()`方法尝试连接服务器 `(server_ip, server_port)`。成功连接后,利用`send()`和`recv()`进行数据交互,记得在通信完成后调用`close()`关闭连接,确保资源释放和程序稳定性。
|
1月前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
148 0
|
23天前
|
负载均衡 网络协议 Java
构建高效可扩展的微服务架构:利用Spring Cloud实现服务发现与负载均衡
本文将探讨如何利用Spring Cloud技术实现微服务架构中的服务发现与负载均衡,通过注册中心来管理服务的注册与发现,并通过负载均衡策略实现请求的分发,从而构建高效可扩展的微服务系统。
|
1月前
|
Cloud Native Java 开发者
Spring Boot 4.0:构建云原生Java应用的前沿工具
Spring Boot 4.0:构建云原生Java应用的前沿工具
|
1月前
|
Java API Maven
使用Java和Spring Boot构建RESTful API
使用Java和Spring Boot构建RESTful API
16 0
|
2月前
|
网络协议 Java API
【JavaEE初阶】 TCP服务器与客户端的搭建
【JavaEE初阶】 TCP服务器与客户端的搭建
|
2月前
|
自然语言处理 Java 编译器
【JavaEE初阶】 UDP服务器与客户端的搭建
【JavaEE初阶】 UDP服务器与客户端的搭建

推荐镜像

更多