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="com.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= "com.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完成
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值