webservice接口示例(spring+xfire+webservice)

webservice接口示例(spring+xfire+webservice)

CreateTime--2018年4月2日17:36:07

Author:Marydon

1、準備工做

  1.1 jar包html

  springjar包,xfire jar包,webservicejar包 java

  1.2 目錄結構web

2、代碼設計

  web.xmlspring

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>demo_WebService</display-name>
  
  <!-- spring的核心配置文件 -->
   <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:spring-applicationContext.xml</param-value>
   </context-param>
   
  <!-- spring的監聽器,用於在服務器開啓的時會自動加載spring的配置文件 -->
  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
  <!-- xfire的核心servlet,用於發佈webservice服務 -->
  <servlet>
    <servlet-name>XFireServlet</servlet-name>
    <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
<!--     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> -->
    <!-- 用於標記容器是否在啓動的時候就加載這個servlet -->
    <!-- 當值爲0或者大於0時,表示容器在應用啓動時就加載這個servlet;
       當是一個負數時或者沒有指定時,則指示容器在該servlet被選擇時才加載。
       正數的值越小,啓動該servlet的優先級越高。
       若是咱們在web.xml中設置了多個servlet的時候,能夠使用load-on-startup來指定servlet的加載順序,服務器會根據load-on-startup的大小依次對servlet進行初始化。不過即便咱們將load-on-startup設置重複也不會出現異常,服務器會本身決定初始化順序。
    -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <!-- 在這個URI下開放WebService服務,即該路徑下的全部請求由XFireSpringServlet接管 -->
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

  spring-applicationContext.xmlapache

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <!--加載webservice 組件的配置-->
    <import resource="config/spring-webService.xml"/>
    
    
</beans>

  spring-webService.xml瀏覽器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    
    <!-- 引入XFire預配置信息 -->
    <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
    
    <!-- 接口業務實現 -->
    <bean id="servicTestImpl" class="webService.impl.ServiceTestImpl">
       <!-- 這裏能夠配置數據層 -->
<!--         <constructor-arg index="0" ref="daoVIRTUAL_CARD"/> -->
    </bean>
    
    <!-- 
        java接口類的名稱就是webservice的(訪問該接口的)名稱,沒法自定義接口的名稱
        class-使用XFire導出器
     -->
    <bean id="myFirstInterface" class="org.codehaus.xfire.spring.remoting.XFireExporter">
        <!-- 引用xfire.xml中定義工廠 -->
        <property name="serviceFactory" ref="xfire.serviceFactory"/>
        <!-- 引用xfire.xml中定義的xfire實例 -->
        <property name="xfire" ref="xfire"/>
        <!-- 提供服務接口的實現類 -->
        <property name="serviceBean" ref="servicTestImpl" />
        <!-- 提供服務的接口,這個接口是向客戶端進行暴露,裏面的方法均可以被客戶端所訪問 -->
        <property name="serviceClass" value="webService.IServiceTest"/>
    </bean>
    
</beans>

  IServiceTest.java服務器

package webService;

/**
 * webservice接口測試
 * @author Marydon
 * @createTime 2018年3月9日上午9:32:11
 * @updateTime
 * @Email:Marydon20170307@163.com
 * @version:1.0.0
 */
public interface IServiceTest {
    /**
     * 獲取用戶信息
     * @return
     */
    public String getUserInfo(String xml);
}

  ServiceTestImpl.javamvc

package webService.impl;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;

import webService.IServiceTest;

/**
 * webservice接口測試實現類
 * @author Marydon
 * @createTime 2018年3月9日上午9:38:09
 * @updateTime
 * @Email:Marydon20170307@163.com
 * @version:1.0.0
 */
public class ServiceTestImpl implements IServiceTest {

    @Override
    public String getUserInfo(String xml) {
        // 返回xml信息
        String xmlResult = "";
        try {
            // 1.解析xml,獲取document對象
            Document document = DocumentHelper.parseText(xml);
            System.out.println(document.asXML().toString());
            // 獲取參數id的值
            String id = document.selectSingleNode("//userId").getText();
            
            // 2.參數校驗
            if (!StringUtils.isEmpty(id)) {
                StringBuffer sb = new StringBuffer();
                sb.append("<Response>");
                // 姓名
                sb.append("<userName>").append("Marydon").append("</userName>");
                sb.append("<error_text>").append("成功").append("</error_text>");
                sb.append("</Response>");
                Document bodyDoc = DocumentHelper.parseText(sb.toString());
                xmlResult = bodyDoc.getRootElement().asXML();
                System.out.println(xmlResult);
            } else {
                // 返回錯誤信息
                xmlResult = this.returnErrorXml("id爲空!");
            }
            System.out.println(xmlResult);
            
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        
        return xmlResult;
    }
    
    /**
     * 返回錯誤信息
     * @param errorMsg
     * @return
     * @throws DocumentException
     */
    private static String returnErrorXml(String errorMsg) throws DocumentException {
        
        // 返回xml信息
        String xmlResult = "";
        StringBuilder sb = new StringBuilder();
        sb.append("<Response>");
        sb.append("<error_text>").append(errorMsg).append("</error_text>");
        sb.append("</Response>");
        Document bodyDoc = DocumentHelper.parseText(sb.toString());
        xmlResult = bodyDoc.getRootElement().asXML();
        System.out.println("XML:" + xmlResult);
        return xmlResult;
    }
}

3、測試階段

  1.是否接口是否發佈成功:app

  在瀏覽器中打開上面的接口地址:http://127.0.0.1:8070/demo/services/IServiceTest?wsdl,dom

  若是出現wsdl文檔,則表示發佈成功。

  2.測試數據傳輸:

  方法一:藉助工具SoapUI

  方法二:使用java藉助xfire調用接口

  相關推薦裏↓↓↓有介紹。

4、總結

  使用xfire發佈webservice服務有兩種方式:

  1.使用org.codehaus.xfire.spring.XFireSpringServlet來管理webservice請求
  使用這種方式,/services,即http://127.0.0.1:8070/demo_WebService/services
  會顯示出項目下全部的接口信息
  另外,接口的名稱和定義的Java接口名稱一致,其訪問路徑就是:接口名稱+"?wsdl"
  而且,接口的訪問路徑沒法自定義修改  

  2.使用org.springframework.web.servlet.DispatcherServlet來管理webservice請求
  使用這種方式,/services,即http://127.0.0.1:8070/demo_WebService2/services
  不會顯示出項目下全部的接口信息
  另外,接口的名稱和設置該java接口類的bean注入的name屬性的值相對應,name的值+"?wsdl"
  修改name的值,就能夠改變該接口所對應的訪問路徑

  使用spring來管理webservice請求的例子也已經寫好,加qq付費獲取源碼。

相關文章
相關標籤/搜索