美文网首页
基于xfire+spring+maven构建webservice

基于xfire+spring+maven构建webservice

作者: 誓俭草 | 来源:发表于2019-02-22 14:44 被阅读0次

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

    本人由于最近项目需要设计一个webservice相关接口,虽然这种接口方式已经逐渐被restful接口取代,但有时候也是需要用到的。这不我之前一直用restful接口,现在就被安排了一个webservice接口,自己顺带着又学习了一把。
    1. 首先要搭建一个web项目用来开发webservice服务端,我们采用maven项目来搭建,方便jar包管理:
      先上pom.xml文件:
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.ljj</groupId>
     <artifactId>xfireServer</artifactId>
     <packaging>war</packaging>
     <version>0.0.1-SNAPSHOT</version>
     <name>xfireServer Maven Webapp</name>
     <url>http://maven.apache.org</url>
     <dependencies>
       <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>org.codehaus.xfire</groupId>
           <artifactId>xfire-core</artifactId>
           <version>1.2.5</version>
       </dependency> 
       <dependency>
           <groupId>org.codehaus.xfire</groupId>
           <artifactId>xfire-spring</artifactId>
           <version>1.2.6</version>
       </dependency> 
       <dependency>  
         <groupId>org.springframework</groupId>  
         <artifactId>spring-web</artifactId>  
         <version>3.2.8.RELEASE</version>  
       </dependency>  
     </dependencies>
     <build>
       <finalName>xfireServer</finalName>
     </build>
    </project>
    

    这里需要引入xfire与spring的相关jar包。

    1. 接下来需要配置web.xml文件
    <!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
     <display-name>Archetype Created Web Application</display-name>
     <listener>
             <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       </listener>
       <context-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:spring/*.xml</param-value>
       </context-param>
       <!-- springmvc 上下文监听器  ContextLoaderListener-->
        <servlet>  
         <servlet-name>xfire</servlet-name>  
         <servlet-class>  
          org.codehaus.xfire.spring.XFireSpringServlet  
         </servlet-class>  
        </servlet>  
        <servlet-mapping>  
         <servlet-name>xfire</servlet-name>  
         <url-pattern>/service/*</url-pattern>  
        </servlet-mapping>  
    </web-app>
    

    需要配置spring上下文监听器和xfire的servert配置
    3.接下来需要开发对应的service方法类了,例如:
    接口类:

    package com.ljj.service;
    
    public interface BookService {
      public String getBook(String xml);
    }
    

    接口实现类:

    package com.ljj.serviceimpl;
    
    import com.ljj.service.BookService;
    
    public class BookServiceImpl implements BookService {
    
      public String getBook(String xml) {
        return xml;
      }
    
    }
    

    4.业务处理service类写完之后,就需要配置xfire与service之前的关联了
    新建applicationContext.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
        <!--引入xfire配置-->
         <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />   
         <!-- 使用XFire导出器 我们通过XFireExporter将业务类导出为Web Service,对于任何导出器,
         我们都需要引入XFire环境,即serviceFactory和xfire,这是标准的配置。ServiceFactory是
         XFire的核心类,它可以将一个POJO生成为一个Web Service。在本实例中,我们通过定义一个
         baseWebService,其余的webService配置都将该bean作为父bean,这样可以简化Spring的配置,
         不需要多次引入serviceFactory和xfire。-->
          <bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true">    
          <!-- 引用xfire.xml中定义的工厂 -->
           <property name="serviceFactory" ref="xfire.serviceFactory" />    
           <!-- 引用xfire.xml中的xfire实例 -->
           <property name="xfire" ref="xfire" />   
          </bean>   
          <!-- 配置业务服务service类 -->
          <bean id="bookWS" class="com.ljj.serviceimpl.BookServiceImpl"> 
          </bean>   
          <bean id="bookService" parent="baseWebService">    
          <!-- 业务服务bean -->
           <property name="serviceBean" ref="bookWS" />    
            <!-- 业务服务bean的对应接口类 -->
           <property name="serviceClass" value="com.ljj.service.BookService" />   
          </bean> 
    </beans>
    

    5.到这里webservice服务端基本完成,需要简单测试一下:
    启动服务后在浏览器访问:http://localhost:8080/xfireServer/service/BookService?wsdl看到如下页面即为成功

    <wsdl:definitions xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://service.ljj.com" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" targetNamespace="http://service.ljj.com">
    <wsdl:types>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service.ljj.com">
    <xsd:element name="getBook">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string"/>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    <xsd:element name="getBookResponse">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string"/>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    </xsd:schema>
    </wsdl:types>
    <wsdl:message name="getBookRequest">
    <wsdl:part name="parameters" element="tns:getBook"> </wsdl:part>
    </wsdl:message>
    <wsdl:message name="getBookResponse">
    <wsdl:part name="parameters" element="tns:getBookResponse"> </wsdl:part>
    </wsdl:message>
    <wsdl:portType name="BookServicePortType">
    <wsdl:operation name="getBook">
    <wsdl:input name="getBookRequest" message="tns:getBookRequest"> </wsdl:input>
    <wsdl:output name="getBookResponse" message="tns:getBookResponse"> </wsdl:output>
    </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="BookServiceHttpBinding" type="tns:BookServicePortType">
    <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="getBook">
    <wsdlsoap:operation soapAction=""/>
    <wsdl:input name="getBookRequest">
    <wsdlsoap:body use="literal"/>
    </wsdl:input>
    <wsdl:output name="getBookResponse">
    <wsdlsoap:body use="literal"/>
    </wsdl:output>
    </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="BookService">
    <wsdl:port name="BookServiceHttpPort" binding="tns:BookServiceHttpBinding">
    <wsdlsoap:address location="http://10.1.33.96:8080/xfireServer/service/BookService"/>
    </wsdl:port>
    </wsdl:service>
    </wsdl:definitions>
    

    6.编写测试客户端——同样适用maven项目,采用动态访问方式:
    pom.xml文件

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.ljj</groupId>
      <artifactId>xfireClient</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>xfireClient</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
        <groupId>org.codehaus.xfire</groupId>
        <artifactId>xfire-all</artifactId>
        <version>1.2.6</version>
        </dependency>
      </dependencies>
    </project>
    

    client测试代码:

    package com.ljj.xfireClient;
    
    import java.net.MalformedURLException;
    import java.net.URL;
    import org.codehaus.xfire.client.Client;
    
    /**
     * Hello world!
     *
     */
    public class App {
      public static void main(String[] args) throws MalformedURLException, Exception {
        System.out.println("Hello World!");
        Client client =
            new Client(new URL("http://127.0.0.1:8080/xfireServer/service/BookService?wsdl"));
        Object[] results2 = client.invoke("getBook", new Object[] {"success"});
        System.out.println(results2[0]);
      }
    }
    

    7.最后附上程序源码供参考:https://gitee.com/fdwokers/xfireWebservice

    相关文章

      网友评论

          本文标题:基于xfire+spring+maven构建webservice

          本文链接:https://www.haomeiwen.com/subject/aireyqtx.html