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>

Cxf开发webservice服务java类

接口类ISayHello

package webserviceServer; 

import javax.jws.WebParam;
import javax.jws.WebService; @WebService
public interface ISayHello {
public String sayHello(@WebParam(name="name")String name);
}

实现类SayHelloImpl

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;
}
}

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>

Cxf + spring 开发webservice用到的jar包

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

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

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

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

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

使用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

刷新项目显示产生了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");
}
}

Log4j的配置文件

这里配置了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

webserviceURL.properties文件的内容

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>

struts-webserviceTest.xml配置文件的内容

<?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>

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的配置结束 ******************** --> <!-- ****************** 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>

启动cxfWebServiceServer(服务端)JavaEECommonApp(普通的java应用)

访问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服务结束...........

日志文件的内容 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服务结束...........

struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例的更多相关文章

  1. Spring boot+CXF开发WebService

    最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...

  2. Spring boot+CXF开发WebService Demo

    最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...

  3. Java动态调用webService,axis2动态调用webService

    Java动态调用webService axis2动态调用webService >>>>>>>>>>>>>>>& ...

  4. Java调用WebService方法总结(4)--Axis调用WebService

    Axis是比较常用的WebService框架,该项目在2006实现了最终版,后面就没有更新了.文中demo所使用到的软件版本:Java 1.8.0_191.Axis 1.4. 1.准备 参考Java调 ...

  5. iOS 开发之路(登陆验证调用WebService)二

    swift3.0下使用Alamofire调用Webservice遇到的一些问题以及解决方案. 首先是针对没有证书的https下的接口处理问题(ps:不推荐在正式版本中使用),manager.reque ...

  6. Java调用WebService方法总结(6)--XFire调用WebService

    XFire是codeHaus组织提供的一个WebService开源框架,目前已被Apache的CXF所取代,已很少有人用了,这里简单记录下其调用WebService使用方法.官网现已不提供下载,可以到 ...

  7. java接口调用——webservice就是一个RPC而已

    很多新手一听到接口就蒙逼,不知道接口是什么!其实接口就是RPC,通过远程访问别的程序提供的方法,然后获得该方法执行的接口,而不需要在本地执行该方法.就是本地方法调用的升级版而已,我明天会上一篇如何通过 ...

  8. java如何调用webservice接口

    java调用WebService可以直接使用Apache提供的axis.jar自己编写代码,或者利用Eclipse自动生成WebService Client代码,利用其中的Proxy类进行调用.理论上 ...

  9. 一个php创建webservice,并通过c#调用的真实实例

    最近需要用php创建webservice供C#和JAVA来调用,通过3天的搜索和尝试,终于成功在C#下调用,JAVA的调用还没开始,为防止忘记,在这里记录下来全过程. 本文参考了许多文章,文中也采用了 ...

随机推荐

  1. VMware vSphere 5.1 简介与安装

    虚拟化系列-VMware vSphere 5.1 简介与安装  标签: 虚拟化 esxi5.1 VMware vSphere 5.1 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 . ...

  2. Android中EditText样式修改 聚焦光标、背景

    在Android开发中,根据项目的需求,需要定制一些特殊的样式,例如:使用EditText时,聚焦时的背景及光标图片使用自定义而非android系统默认的.这两天,在项目中涉及此需求,现记录如下: 首 ...

  3. AngularJS-Controller的使用-读书笔记

    最近在读<Angular JS权威教程>读到第9页,按着示例做,居然报错,说MyController undefined,初学者不懂啊,找了个官方的文档,按着改了一下,貌似成功了,有需要的 ...

  4. php乱码

    1:php 编码是utf-8 但是接口需要是gbk 这么办?? $message = "你的微点管理地址为:".$shortUrl." [请保存信息]"; // ...

  5. ArcServer 10.0 “No Content”、“Server Host Cannot be null” 错误

    问题一:"No Content" 问题描述: 在输入服务地址时:http://192.168.1.180/arcgis/services   结果出现下面的错误:   解决办法: ...

  6. os.popen(command)

    command="/usr/local/sbin/xxx_cmd" os.popen(command) xxx_cmd是自己编译的二进制文件,如果不加上全路径/usr/local/ ...

  7. [游戏模版21] Win32 物理引擎 能量守恒

    >_<:Only a little change in the function of MyPaint(...),besides the initial value have some c ...

  8. Nutz Dao实体中索引注解的使用(@TableIndexes@Index)

    Nutz是一组轻便小型的框架的集合, 各个部分可以被独立使用,把SSH的精华封装在一个1M左右的jar包中,Nutz不对其他任何第三方库产生依赖,如果不考虑数据库链接和日志的话,创建完美的Web应用只 ...

  9. [BTS]The join order has been enforced because a local join hint is used.;Duplicate key was ignored.".

    在一个客户的BizTalk Server 2013 R2环境中会报如下的ERROR,查找相关资料后,先试试停掉所有Trace. Log Name:      ApplicationSource:    ...

  10. [BTS] RFC IDOC_INBOUND_ASYNCHRONOUS

    Error Message: Log Name:      ApplicationSource:        BizTalk ServerDate:          9/10/2013 3:56: ...