您的位置:首页 > 编程语言 > Java开发

【WebService框架-CXF】——CXF+Spring+Struts+自定义拦截器构建WebService客户端

2016-06-20 21:00 579 查看
  在上一篇博客中我们总结了在SSH架构的服务端项目中添加Web Service层,并发布服务。这篇文章中,我们将介绍如何结合Spring构建WebService的客户端。

步骤

1.新建Java Web Project

2.引入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>
标签。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: