码迷,mamicode.com
首页 > 编程语言 > 详细

spring实现WebService

时间:2015-01-03 11:56:54      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:webservice   spring   

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完成

spring实现WebService

标签:webservice   spring   

原文地址:http://blog.csdn.net/rongku/article/details/42361043

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!