【WebService框架-CXF】——CXF+Spring+Struts+自定义拦截器构建WebService客户端
2016-06-20 21:00
579 查看
在上一篇博客中我们总结了在SSH架构的服务端项目中添加Web Service层,并发布服务。这篇文章中,我们将介绍如何结合Spring构建WebService的客户端。
2.引入Spring,Struts,CXF的相关Jar包。
3.通过java2wsdl命令生成客户端代理
链接地址为http://localhost:8080/CXF_Spring/webservice/HelloWorldWS?wsdl
4.编写Action,调用服务
将Action配置到struts.xml中
ListCats.jsp
将Action交由Spring来管理。
5.在applicationContext.xml中配置拦截器
步骤
1.新建Java Web Project2.引入Spring,Struts,CXF的相关Jar包。
3.通过java2wsdl命令生成客户端代理
链接地址为http://localhost:8080/CXF_Spring/webservice/HelloWorldWS?wsdl
4.编写Action,调用服务
public class ListCatAction extends ActionSupport { private HelloWorld hw; private Map<String,Cat> cats=new HashMap<String, Cat>(); public Map<String, Cat> getCats() { return cats; } public void setCats(Map<String, Cat> cats) { this.cats = cats; } public void setHw(HelloWorld hw) { this.hw = hw; } @Override public String execute() throws Exception { StringCat sc=hw.getAllCats(); if(sc!=null && sc.getEntries()!=null){ for(Entry e:sc.getEntries()){ cats.put(e.getKey(), e.getValue()); } } return "SUCCESS"; } }
将Action配置到struts.xml中
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <package name="struts2" extends="struts-default"> <action name="ListCatAction" class="ListCatAction"> <result name="SUCCESS">/WEB-INF/ListCats.jsp</result> </action> </package> </struts>
ListCats.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>All the cats</title> </head> <body> <s:iterator value="cats" var="entry"> <li>${entry.key}-->${entry.value.name}</li> </s:iterator> </body> </html>
将Action交由Spring来管理。
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://cxf.apache.org/jaxws http://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="ListCatAction" class="com.tgb.action.ListCatAction"> <property name="hw" ref="hw"></property> </bean> </beans>
5.在applicationContext.xml中配置拦截器
<jaxws:client id="hw" serviceClass="com.tgb.ws.HelloWorld" address="http://localhost:9009/HelloWorldWS" > <jaxws:outInterceptors> <bean class="com.tgb.ws.auth.AddHeaderInterceptor"> <constructor-arg value="1"></constructor-arg> <constructor-arg value="123"></constructor-arg> </bean> </jaxws:outInterceptors> </jaxws:client>
总结
这里不同于服务端的配置就是使用<jaxws:client>标签。
相关文章推荐
- java 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 【JAVA学习】Core Java读书记录
- java集合框架(一)
- Java构造函数
- Java正则表达式
- java动态代理
- Eclipse的使用小技巧和快捷键演示
- leetcode: 4. Median of Two Sorted Arrays (java)
- NYOJ-16 矩形嵌套
- JDBC
- Eclipse Mars中Cordova 开发
- leetcode:3. Longest Substring Without Repeating Characters (java)
- 简述spring 的事务传播行为和 隔离级别
- java 是如何进行异常处理的
- JAVA的各种O
- Java中静态关键字的使用方法介绍一
- SpringMVC学习--文件上传
- Spring事务学习记录
- leetcode: 2. Add Two Numbers (java)
- java链接mysql以及对数据库的增,删,改,查