xfire+spring+maven构建webservice服务器和客户端 您所在的位置:网站首页 webservice服务端并发 xfire+spring+maven构建webservice服务器和客户端

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

2024-07-04 02:35| 来源: 网络整理| 查看: 265

本文转自: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代码  收藏代码

    

    

    

    webservice    

      

      

          org.springframework.web.context.ContextLoaderListener  

      

      

        contextConfigLocation  

        classpath:spring/*.xml  

      

      

         

      xfire    

          

       org.codehaus.xfire.spring.XFireSpringServlet    

          

         

         

      xfire    

      /service/*    

         

  

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

Xml代码  收藏代码

  

  

  

             

               

                

               

              

             

              

               

                

               

            

    

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

book.java

Java代码  收藏代码

package com.xiaoji.webservice.xfire.entity;  

  

public class Book {  

  

    private int bookId;  

    private String name;  

      

    public Book(){  

          

    }  

      

    public Book(int bookId,String name){  

        this.bookId = bookId;  

        this.name = name;  

    }  

  

    public int getBookId() {  

        return bookId;  

    }  

  

    public void setBookId(int bookId) {  

        this.bookId = bookId;  

    }  

  

    public String getName() {  

        return name;  

    }  

  

    public void setName(String name) {  

        this.name = name;  

    }  

      

      

}  

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

Java代码  收藏代码

package com.xiaoji.webservice.xfire.service;  

  

  

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

  

public interface BookService {  

  

    public Book getBookById(int id);  

    public String sayHello(String str);  

}  

 

Java代码  收藏代码

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

  

import java.util.ArrayList;  

import java.util.List;  

  

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

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

  

public class BookServiceImpl implements BookService {  

  

    public Book getBookById(int id) {  

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

        return book;  

    }  

  

    public String sayHello(String str) {  

        // TODO Auto-generated method stub  

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

    }  

  

  

}  

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

Java代码  收藏代码

  

  4.0.0  

  com.xiaoji.webservice  

  webservice-xfire  

  war  

  0.0.1-SNAPSHOT  

  webservice-xfire Maven Webapp  

  http://maven.apache.org  

    

      

      junit  

      junit  

      3.8.1  

      test  

      

      

    org.codehaus.xfire  

    xfire-core  

    1.2.5  

       

      

        org.codehaus.xfire  

        xfire-spring  

        1.2.6  

       

        

      org.springframework    

      spring-web    

      3.2.8.RELEASE    

        

        

    

    webservice-xfire  

    

  

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代码  收藏代码

package com.xiaoji.webservice.xfire.client;  

  

import java.net.MalformedURLException;  

  

  

  

public class WebserviceXfireClient {  

  

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

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

        //xfire自动生成客户端  

        BookServiceClient client = new BookServiceClient();  

        BookServicePortType bs = client.getBookServiceHttpPort();  

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

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

    }  

      

}  

 

最后贴上xfire的pom.xml:

Java代码  收藏代码

  

  4.0.0  

  com.xiaoji.webservice.xfire  

  eclipseplugin  

  war  

  0.0.1-SNAPSHOT  

  eclipseplugin Maven Webapp  

  http://maven.apache.org  

    

        

    org.codehaus.xfire  

    xfire-all  

    1.2.6  

  

    

    

    eclipseplugin  

    

  

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

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

Java代码  收藏代码

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

public static void testClient1() throws MalformedURLException {  

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

  

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

  

    XFireProxyFactory factory = new XFireProxyFactory(xfire);  

  

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

  

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

  

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

  

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

}  

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

Java代码  收藏代码

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

    public static void testClient2() throws MalformedURLException, Exception {  

        Client client = new Client(  

                new URL(  

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

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

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

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

        //返回对象解析  

        Document xmlTree = (Document)results2[0];  

        Element root = xmlTree.getDocumentElement();  

        parseElement(root);  

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

    }  

  

    /** 

     * 解析返回树 

     * @param root 

     */  

    private static void parseElement(Element root) {  

        String key = root.getNodeName();  

        NodeList list = root.getChildNodes();  

        for (int i = 0; i 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有