struts1+spring+myeclipse +cxf 開發webservice以及普通java應用調用webservice的實例

Cxf + Spring+ myeclipse+ cxf 進行  Webservice服務端開發

使用Cxf開發webservice的服務端項目結構

Spring配置文件applicationContext.xml 介紹:

配置文件的位置:
src/config/spring/applicationContext.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:jaxws="http://cxf.apache.org/jaxws" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
         http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> 
    <!--                        建立要發佈webservice服務的實例bean對象                                                         -->
    <bean id="sayHelloManager" class="webserviceServer.SayHelloImpl" /> 
    <!--                              發佈webservice服務                                                                                            -->
    <jaxws:endpoint id="sayHello" implementor="#sayHelloManager" address="/sayHelloServer" />
 </beans> 
View Code

Cxf開發webservice服務java類

接口類ISayHellojava

package webserviceServer; 

import javax.jws.WebParam; 
import javax.jws.WebService; 

@WebService 
public interface ISayHello { 
    public String sayHello(@WebParam(name="name")String name); 
}
View Code

實現類SayHelloImplweb

package webserviceServer; 
import javax.jws.WebService; 

//注意這裏的websservice聲明格式
//@WebService(endpointInterface = "接口的全類名,不是接口實現類的全類名")

@WebService(endpointInterface = "webserviceServer.ISayHello") 
public class SayHelloImpl implements ISayHello { 
    public String sayHello(String name){ 
        return "hello every one, my name is " + name; 
    } 
}
View Code

Web.xml的內容

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
<!--                          spring的配置開始                                                                       -->    
    <context-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value> 
          classpath:config/spring/applicationContext.xml 
      </param-value> 
  </context-param> 
  
  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
<!--                          spring的配置結束                                                                      -->    

<!--                            cxf的配置開始                                                                       -->        
   <servlet> 
       <servlet-name>CXFServlet</servlet-name> 
       <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup> 
   </servlet> 

   <servlet-mapping> 
       <servlet-name>CXFServlet</servlet-name> 
       <url-pattern>/service/api/*</url-pattern> 
   </servlet-mapping> 
<!--                            cxf的配置結束                                                                       -->            
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
View Code

Cxf + spring 開發webservice用到的jar包

啓動webservice的服務端進行查看能夠訪問的發佈的服務

http://localhost:8080/cxfWebServiceServer/service/apispring

J2EE普通項目調用cxf開發的webservice

新建普通的java應用JavaEECommonApp,調用webservice

這裏的項目中尚未webservice的客戶端apache

使用jdk自帶的命令wsimport 生成webservice的客戶端

在cmd命令提示符中進入到JavaEECommonApp的src目錄下面
使用以下命令:
wsimport -keep -p com.waggd.webservie.client  http://localhost:8080/cxfWebServiceServer/service/api/sayHelloServer?wsdl 
來生成客戶端
命令參數說明:
  -d:生成客戶端執行類的class文件的存放目錄
  -s:生成客戶端執行類的源文件的存放目錄
 -p:定義生成類的包名 ,這裏使用com.waggd.webservie.client 
View Code

刷新項目顯示產生了webservice的客戶端

在struts1+spring的java項目中調用webservice

Struts1的action代碼

package com.wanggd.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.sinovatech.common.web.action.BaseAdmAction;      
import com.waggd.webservie.client.ISayHello;

/**
 * BaseAdmAction 是自定義的類繼承 了org.apache.struts.actions.MappingDispatchAction;
 * @author Administrator
 *
 */
public class webserviceCallTest extends BaseAdmAction {
    
    private static final Log log = LogFactory.getLog(webserviceCallTest.class);
    private ISayHello sayHelloManager;
    
    public ActionForward callWebServiceTest(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        log.info("調用webservice服務開始...........");
        log.info("sayHelloManager.sayHello('wanggd')輸出結果 :  "+sayHelloManager.sayHello("wanggd"));
        log.info("調用webservice服務結束...........");
        return null;
    }
    
    public void init(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        sayHelloManager = (ISayHello) this.getBeanContext().getBean("webserviceManager");
    }
} 
View Code

Log4j的配置文件api

這裏配置了2個日誌的輸出目的地,一個輸出到文件,一個輸出到控制檯,日誌輸出級別是info

log4j.rootLogger = INFO,A3,CONSOLE


# - INFO File Appender
# - ------------------------------------------------------------------------------------
log4j.appender.A3.Threshold=INFO
log4j.appender.A3.encoding=UTF-8
log4j.appender.A3 = org.apache.log4j.DailyRollingFileAppender
log4j.appender.A3.File=c:/logtest/j2eeCommonApp.log
log4j.appender.A3.ImmediateFlush=true
log4j.appender.A3.DatePattern='_'yyyy-MM-dd
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=%-d{yyyy/MM/dd HH:mm:ss} OSS %-5p [%c] - %m%n


log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=INFO
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-d{yyyy/MM/dd HH:mm:ss} OSS %-5p [%c] - %m%n
View Code

webserviceURL.properties文件的內容app

webserviceServerURL=http://localhost:8080/cxfWebServiceServer/service/api/sayHelloServer?wsdl框架

applicationContext.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:jaxws="http://cxf.apache.org/jaxws" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
         http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> 
<!--                               讀取保存webserviceURL的properties                       -->
<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:config/properties/webserviceURL.properties</value>
            </list>
        </property>
    </bean>
<!-- 在普通的java應用中獲取webservice服務的實例對象 -->
<jaxws:client id="webserviceManager" address="${webserviceServerURL}"  
serviceClass="com.waggd.webservie.client.ISayHello" />
 </beans>
View Code

struts-webserviceTest.xml配置文件的內容eclipse

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config> 
  <action-mappings>
      <action path="/webservice/*" type="com.wanggd.action.webserviceCallTest" 
       scope="request" parameter="{1}">
        <forward name="list" path="/listUnifiedorderAuth.jsp"/>
      </action>
  </action-mappings>
</struts-config>
View Code

Web.xml的內容webapp

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--  ******************              spring的配置開始            ******************** -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value> 
          classpath:config/spring/applicationContext.xml 
      </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
<!--  ******************              spring的配置結束            ******************** -->

<!--  ******************              log4j的配置開始            ********************  -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:config/properties/log4j.properties</param-value>
    </context-param>

    <!--
        每隔多少毫秒掃描一下配置文件的變化(此參數可選配) <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>60000</param-value> </context-param>
    -->
    <!--
        spring框架默認定義webAppRootKey的值爲webapp.root,
        若不配此參數默認值就是webapp.root(所以,此參數可選配)
        <context-param> <param-name>webAppRootKey</param-name>
        <param-value>home</param-value> </context-param>
    -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
<!--  ******************              log4j的配置結束            ********************  -->

<!--  ******************              struts1的配置開始            ********************  -->
    <servlet>
        <servlet-name>strutsOne</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>
                /WEB-INF/classes/config/struts/struts-webserviceTest.xml
            </param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>strutsOne</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
<!--  ******************              struts1的配置結束            ********************  -->
</web-app>
View Code

啓動cxfWebServiceServer(服務端)JavaEECommonApp(普通的java應用)jsp

訪問JavaEECommonApp(普通的java應用)

http://localhost:8080/JavaEECommonApp/webservice/callWebServiceTest.do

控制檯輸出日誌:

2013/12/06 10:59:52 OSS INFO  [com.wanggd.action.webserviceCallTest] - 調用webservice服務開始...........
2013/12/06 10:59:53 OSS INFO  [com.wanggd.action.webserviceCallTest] - sayHelloManager.sayHello('wanggd')輸出結果 :  hello every one, my name is wanggd
2013/12/06 10:59:53 OSS INFO  [com.wanggd.action.webserviceCallTest] - 調用webservice服務結束...........
View Code

日誌文件的內容 C:\logtest\ j2eeCommonApp.log

2013/12/06 10:54:12 OSS INFO  [com.wanggd.action.webserviceCallTest] - sayHelloManager.sayHello('wanggd')輸出結果 :  hello every one, my name is wanggd
2013/12/06 10:54:12 OSS INFO  [com.wanggd.action.webserviceCallTest] - 調用webservice服務結束...........
2013/12/06 10:59:52 OSS INFO  [com.wanggd.action.webserviceCallTest] - 調用webservice服務開始...........
2013/12/06 10:59:53 OSS INFO  [com.wanggd.action.webserviceCallTest] - sayHelloManager.sayHello('wanggd')輸出結果 :  hello every one, my name is wanggd
2013/12/06 10:59:53 OSS INFO  [com.wanggd.action.webserviceCallTest] - 調用webservice服務結束...........
View Code
相關文章
相關標籤/搜索