struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

Cxf + Spring+ myeclipse+ cxf 进行  Webservice服务端开发

使用Cxf开发webservice的服务端项目结构

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

Spring配置文件applicationContext.xml 介绍:

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
配置文件的位置:

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类

接口类ISayHello

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
package webserviceServer; 



import javax.jws.WebParam; 

import javax.jws.WebService; 



@WebService 

public interface ISayHello { 

    public String sayHello(@WebParam(name="name")String name); 

}
View Code

实现类SayHelloImpl

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
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的内容

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
<?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包

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

启动webservice的服务端进行查看可以访问的发布的服务

http://localhost:8080/cxfWebServiceServer/service/api

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

J2EE普通项目调用cxf开发的webservice

新建普通的java应用JavaEECommonApp,调用webservice

这里的项目中还没有webservice的客户端

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

使用jdk自带的命令wsimport 生成webservice的客户端

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用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

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

刷新项目显示产生了webservice的客户端

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

在struts1+spring的java项目中调用webservice

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

Struts1的action代码

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
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的配置文件

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
这里配置了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文件的内容

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

applicationContext.xml文件的内容

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
<?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配置文件的内容

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
<?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的内容

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
<?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应用)

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

访问JavaEECommonApp(普通的java应用)

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

控制台输出日志:

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用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

日志文件的内容 C:\logtest\ j2eeCommonApp.log

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
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

 

你可能感兴趣的:(webservice)