(转)Java实现Web Service过程中处理SOAP Header的问题
网上有篇文章,大致这么说的(如下文),最后我采用的wsimport -XadditionalHeaders的方式。
StrikeIron offers two authentication methods for its for-pay web services, a "SOAP Header" authentication method and a "Non-SOAP Header" one (you can see examples of both in the WSDL section of their U.S. Census Information web service). The non-SOAP header validation method works normally with JAX-WS WSDL-to-Java tools (such as Metro's wsimport and CXF's wsdl2java) because the username and password information is provided as SOAP body request elements and hence parameters will be provided for them in the JAX-WS generated SEI's methods.
However, the SOAP header authentication method relies on implicit SOAP headers. These refer to SOAP headers for requests and responses that are defined in the WSDL binding section instead of the portType section. This is frequently done when the nature of the SOAP header information is metadata only indirectly related to the request and response; in such cases it would be desirable to keep this information separate from the business data referenced in the portType operation. However, implicit headers create a slight problem OOTB with JAX-WS tools, because per the JSR-224 specification WSDL-to-Java code generators only take parameters declared within the portType section into account when generating the SEI method signatures. As a result, necessary SOAP header parameters for making the SOAP request would not be available.
To illustrate this, let's choose one of the Census Information service's operations, say GetAllProfiles_ForZip. Its portType operation refers to two wsdl:messages, one each for request and response:
<wsdl:operation name="GetAllProfiles_ForZip">
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/">
Returns all census information by zip</documentation>
<wsdl:input message="si:GetAllProfiles_ForZipSoapIn" /> <wsdl:output message="si:GetAllProfiles_ForZipSoapOut" />
</wsdl:operation> <wsdl:message name="GetAllProfiles_ForZipSoapIn">
<wsdl:part name="parameters" element="si:GetAllProfiles_ForZip" />
</wsdl:message>
<wsdl:message name="GetAllProfiles_ForZipSoapOut">
<wsdl:part name="parameters" element="si:GetAllProfiles_ForZipResponse" />
</wsdl:message>
The elements in the request and response messages contain just the business data elements directly related to the operation. It is only within the binding section where the metadata SOAP header elements are defined:
<wsdl:operation name="GetAllProfiles_ForZip">
<soap:operation soapAction="http://www.strikeiron.com/GetAllProfiles_ForZip"
style="document"/>
<wsdl:input>
<soap:body use="literal"/>
...validation checks omitted...
<soap:header message="si:LicenseInfoMessage" part="LicenseInfo"
use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
<soap:header message="si:GetAllProfiles_ForZipResponseInfo"
part="ResponseInfo" use="literal"/>
<soap:header message="si:SubscriptionInfoMessage" part="SubscriptionInfo" use="literal"/>
</wsdl:output>
</wsdl:operation>
Since these binding-defined SOAP header declarations are bypassed during wsimport/wsdl2java code generation, the SEI's generated do not supply parameters to allow the SOAP client to provide the authentication information on requests or receive other metadata information on the response:
public com.strikeiron.CensusProfile getAllProfilesForZip(
@WebParam(name = "zip", targetNamespace = "http://www.strikeiron.com")
java.lang.String zip
);
Possible solutions to this issue:
Use special wsimport/wsdl2java option settings to bring in implicit headers. Both CXF's wsdl2java and Metro's wsimport tools offer special flags for bringing in implicit SOAP headers when generating the Java artifacts. This is the easiest and probably most reliable solution.
For Metro's wsimport, use -XadditionalHeaders at the command line or xadditionalHeaders="true" for theant task.
For CXF's wsdl2java, use the -exsh true setting shown on the wsdl2java Wiki page.
Using this extension changes the operation signature to the following:
public com.strikeiron.CensusProfile getAllProfilesForZip(
@WebParam(name = "zip", targetNamespace = "http://www.strikeiron.com")
java.lang.String zip,
@WebParam(name = "LicenseInfo", targetNamespace = "http://ws.strikeiron.com", header = true)
com.strikeiron.ws.LicenseInfo licenseInfo,
@WebParam(mode = WebParam.Mode.OUT, name = "ResponseInfo", targetNamespace =
"http://www.strikeiron.com", header = true)
javax.xml.ws.Holder responseInfo,
@WebParam(mode = WebParam.Mode.OUT, name = "SubscriptionInfo", targetNamespace =
"http://ws.strikeiron.com", header = true)
javax.xml.ws.Holder subscriptionInfo
);Modify the WSDL prior to calling the WSDL-to-Java tool, or subsequently alter the generated classes to make the SOAP Headers explicit. This forum topic has an example of the former and blog entry of the latter. This seems tricky and cumbersome to do, however, and unless the flag options above are not working properly for a certain WSDL I wouldn't see any benefit of this over the first option.
Create a JAX-WS Handler and use SAAJ to add the SOAP Headers. SAAJ is a DOM-like API used for creation and manipulation of SOAP messages. As shown in Ivasena's blog entry, a class implementing the SOAPHandler interface can be used to modify the outgoing SOAP request by adding the SOAP header information. This can be a nicely viable option if you do not wish to encumber each SEI method call with authentication information, and by placing this logic in a handler it can be shared across multiple web service calls that need the same headers added. See JAX-WS handler tutorial for more information, and also the RPC/encoded article for examples of working with SAAJ. CXF provides interceptors as an additional option for adding SOAP headers.
(Metro only) Use the WSBindingProvider class to add the SOAP headers. As shown in the JAX-WS user's guide, Metro provides a WSBindingProvider that is implemented by proxy or dispatch objects. Methods on this interface allow for convenient adding of SOAP headers.
(转)Java实现Web Service过程中处理SOAP Header的问题的更多相关文章
- java axis web service
编写 java调用web service的客户端比较简单,其中webservice为上一篇gsoap创建的server. package clientTest; import java.rmi.Rem ...
- <<Java RESTful Web Service实战>> 读书笔记
<<Java RESTful Web Service实战>> 读书笔记 第一章 JAX-RS2.0入门 REST (Representational State ransf ...
- 【转】基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)
转载:http://www.cnblogs.com/windwithlife/archive/2013/03/03/2942157.html 一,选择一个合适的,Web开发环境: 我选择的是Eclip ...
- SOAP: java+xfire(web service) + php客户端
作者: 吴俊杰 web service这项技术暂不说它有多落伍,但是项目中用到了,没法逃避! xml和json各有各的好处,但是JSON无疑是当今数据交互的主流了.客户soap服务器端用的是 j ...
- JAVA开发Web Service几种框架介绍
郑重声明:此文为转载来的,出处已不知了,侵告删. 在讲Web Service开发服务时,需要介绍一个目前开发Web Service的几个框架,分别为Axis,axis2,Xfire,CXF以及JWS( ...
- Java RESTful Web Service相关概念
原文地址:http://1.liangtao.sinaapp.com/?p=647 接上一篇文章REST|RESTful初步认识:p=639">http://1.liangtao.si ...
- (转)JAVA 调用Web Service的三种方法
1.使用HttpClient用到的jar文件:commons-httpclient-3.1.jar方法:预先定义好Soap请求数据,可以借助于XMLSpy Professional软件来做这一步生成. ...
- Java Restful Web Service 学习指南
Restful是一种架构style,目前常说的有restful web service, resultful http.现在热搜榜的微服务,大多数会采用Restful方式. JAX-RS 作为一个Re ...
- Java连接远程Mysql过程中遇到的各种问题
2018-11-16 10:46 2018-11-19 21:35 前言 本篇文章记录的是本人在使用Java程序连接另一台电脑(同一局域网)上的Mysql数据库的过程中遇到的各种问题及解决方案.希望能 ...
随机推荐
- eclipse jdk安装
在Ubuntu16.04.4安装jdk 转载自:http://www.cnblogs.com/zyrblog/p/8510132.html 一.在Ubuntu16.04.4上安装jdk 1.下载jd ...
- Binder学习笔记(八)—— 客户端如何组织Test()请求 ?
还从客户端代码看起TestClient.cpp:14 int main() { sp < IServiceManager > sm = defaultServiceManager(); / ...
- MapReduce Kmeans算法含测试数据
使用时,需要修改K值,args值 运行流程: 先初始化中心点->map中和距离最近的中心点生成一对传入reduce->reduce中把相同key值的存到一起->更新中心点,计算和上一 ...
- [hdu 2604] Queuing 递推 矩阵快速幂
Problem Description Queues and Priority Queues are data structures which are known to most computer ...
- 【Linux】-Ubuntu下配置JDK1.8
前言 这次实在是不想写前言了,好吧,那咱就不写了. 内容 怀着复杂的心情来整理这个小小的操作,其实我的内心是拒绝的,因为太简单了,但是我却花费了很长的时间,有效时间花费了将近两个小时去整理这个小玩意儿 ...
- js中 关于DOM的事件操作
一.JavaScript的组成 JavaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for语句等. DOM:文档对象 ...
- (Python OpenGL)【4】Uniform变量 PyOpenGL
(Python OpenGL) 原文:http://ogldev.atspace.co.uk/www/tutorial05/tutorial05.html(英文) __author__ = " ...
- [SDOI2009]HH的项链 树状数组 BZOJ 1878
题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链 ...
- kuangbin专题七 HDU1754 I Hate It (单点修改维护最大值)
很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有 ...
- [WebShow系列] 需求及环境的确定
需求及环境确定: 主题标语 选手人数 评委人数 打分方式 合计算法 名次算法 大屏数量 大屏分辨率 电脑配置 切换设备 评委人员配备 技术人员配备 现场网速情况 评委移动端配备 场次数量 场次时间 选 ...