【WebService框架-CXF】——CXF+Spring+Struts+自定义拦截器构建WebService客户端
来源:互联网 发布:算法的发展成果 编辑:程序博客网 时间:2024/06/02 23:09
在上一篇博客中我们总结了在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>
标签。
0 0
- 【WebService框架-CXF】——CXF+Spring+Struts+自定义拦截器构建WebService客户端
- 【WebService框架-CXF】——CXF+Spring+自定义拦截器构建WebService服务端
- 【WebService框架-CXF】——CXF拦截器
- WebService-CXF-Spring+自定义拦截器
- webservice cxf 拦截器
- java WebService CXF Spring 自定义拦截器 附实例源码
- webservice框架CXF拦截器的使用
- cxf+Spring的webservice应用--拦截器
- 【WebService框架-CXF】——WebService和CXF
- Spring 部署CXF 框架--WebService
- WebService CXF整合Spring框架
- Spring整合webservice CXF框架
- webservice利用cxf框架编写日志拦截器和自定义拦截器
- 【WebService框架-CXF】——CXF入门实例
- 采用CXF 构建webservice
- CXF构建webservice解析
- 【WebService】CXF拦截器的设置以及自定义CXF拦截器
- WebService学习笔记-CXF添加自定义拦截器
- JavaScript正则表达式上之基本语法
- TablView长按手势执行两次的问题
- 求取一个字符串的最大回文子串
- MVC控制器流程
- spring注解
- 【WebService框架-CXF】——CXF+Spring+Struts+自定义拦截器构建WebService客户端
- Java标准输入输出串流
- Win32游戏制作之---SwordsMan
- byte[ ]数组承接内容时候分配空间
- [Docker]Docker简介
- 字体属性及测量
- java 控制器
- 总结一下常见的几种排序
- 【HDOJ】【百度之星】5699 货物运输