spring实现WebService

Spring+WebService配置

1、web.xml文件添加以下配置

<servlet>
<servlet-name>cxfServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxfServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>

2、applicationContext.xml配置片段

<?xmlversion="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jaxws="http://cxf.apache.org/jaxws"
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/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="wsTestService"class="cn.com.dyninfo.abel.test.ws.impl.WsTestServiceImpl" />
<jaxws:endpoint id="wsTestServiceWs"implementor="#wsTestService"
address="/wsTestService">
</jaxws:endpoint>
……..

3、编写webservice接口,记住加上@WebService标注

packagecn.com.abel.test.ws;
importjavax.jws.WebService;

@WebService
public interface WsTestService{
public String helloWord(String str);
}

4、实现上面的webservice接口

packagecn.com.abel.test.ws.impl;

import javax.jws.WebService;
importorg.springframework.stereotype.Service;
importcn.com.abel.test.ws.WsTestService;
@Service("wsTestService")
//声明web服务,并指定接口路径
@WebService(endpointInterface= "cn.com.dyninfo.abel.test.ws.WsTestService")
public class WsTestServiceImpl implements WsTestService{
    public String hello(String str){
       return "hello" + str;
    }
}

5、启动应用,访问http://localhost:8080/ws/wsTestService?wsdl

如若能正常访问则至此服务端配置完成,下面开始配置客户端

6、把上面服务端的WsTestService接口拷贝过来,包名和类名必须都一致不能修改

7、客户端的applicationContext.xml

<?xmlversion="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jaxws="http://cxf.apache.org/jaxws"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<importresource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:clientid="wsTestService"
serviceClass="cn.com.abel.test.ws.WsTestService"
address="http://localhost:8080/ws/wsTestService" />

6、客户端调用时直接在加上@Resource(name="wsTestService")即可,wsTestService就是上面jaxws:client中的id,如:

@Controller
public class IndexController{
@Resource(name="wsTestService")
private WsTestService wsService;
@RequestMapping("")
public String index(ModelMap model){
  String ss = wsService.helloWord("webservice,this's my first webservice test");

  model.addAttribute("hello", "hellospring mvc, " + ss);
  return "index";
}
}

到此webservice完成

时间: 2024-11-03 21:27:19

spring实现WebService的相关文章

webService总结(三)——使用CXF + Spring发布webService

近些年来,Spring一直很火,许多框架都能跟Spring完美集成,CXF也不例外.下面,我就介绍一下如何使用CXF + Spring发布webService.我们还是使用前两篇博客使用的实例. 服务端: 目录结构: 这里需要的所有Spring的包,CXF的lib目录下都有. IHelloWorldServer代码: package com.test.server; import javax.jws.WebService; @WebService public interface IHelloW

CXF整合Spring开发WebService

刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开环境变量配置窗口,点击新建,新建%CXF_HOME%变量,值为你下载的cxf所在的目录,我的是D:\tools\apache-cxf-3.1.0 2.在Path变量中新加一句%CXF_HOME%\lib,注意要和已有的path变量用;隔开 3.在CLASSPATH中新加一句%CXF_HOME%\li

CXF集成Spring实现webservice的发布与请求

CXF集成Spring实现webservice的发布(服务端) 目录结构: 主要代码: package com.cxf.spring.pojo; public class User { int id ; String name = null; String address = null; String email = null; public int getId() { return id; } public void setId(int id) { this.id = id; } public

axis2+spring开发webservice服务器端

需求:开发VAC与SP间订购通知接口服务器端(SP端),给定VacSyncService_SPClient.wsdl文件 首先,官网下载axis2-1.6.2-bin.zip和axis2-1.6.2-war.zipaxis2-1.6.2-bin.zip包含axis2的jar包,工具和例子axis2-1.6.2-war.zip包含了axis2的web应用,发布web服务时,将自己的程序以特定文件结构发布到axis2的web应用的service目录中 1.根据wsdl生成服务器端代码解压axis2-

一个CXF集成SPRING的WEBSERVICE完整实例

1 首先准备以下JAR包 activation.jar commons-logging-1.1.1.jar cxf-2.5.6.jar jaxb-api-2.2.1.jar jaxb-impl-2.1.3.jar jaxws-api-2.2.8.jar neethi-3.0.2.jar ow2-jws-2.0-spec-1.0.11.jar spring-aop-3.0.5.RELEASE.jar spring-asm-3.0.5.RELEASE.jar spring-beans-3.0.5.R

6.用CXF编写基于Spring的WebService

首先是服务器端: //实体类 public class Weather { private String region;//区域编码 private String regionName;//区域名称 private float temperature;//温度 private boolean isRain;//是否下雨 public Weather() { super(); } public Weather(String region, String regionName, float temp

Spring 整合 webService

创建webservice实现类 @Component @WebService(serviceName="ledgerWebService") public class LedgerWebServcie { @WebMethod public String saveInstituteDispatch( @WebParam(name="unitName") String unitName, @WebParam(name="colonelName")

cxf整合spring实现webservice

前面一篇文章中,webservice的服务端与客户端都是单独启动,但是在现实项目中,服务端单独启动太没有实际意义了,还是要整合框架启动,所以今天将记录如何整合spring框架. jar包下载地址如下: http://yun.baidu.com/share/link?shareid=547689626&uk=2836507213 (一).web.xml中添加spring与cxf的配置 <?xml version="1.0" encoding="UTF-8"

CXF Spring开发WebService,基于SOAP和REST方式

版本CXF2.6.9 添加的包文件 这个版本的不可在Tomcat7上运行,会出错. 配置文件 applicationContext.xml [html] view plain copy <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://ww