webservice调⽤服务端数据时给soapenv:Envelope添加⾃定义
的命名空间
最近做第三⽅接⼝,服务端需要
<soapenv:Envelope xmlns:soapenv="/soap/envelope/" xmlns:web="***/webservices"
xmlns:cus="***/CustomUI">
<soapenv:Header>
<web:SessionType>***</web:SessionType>
<web:PasswordText>***</web:PasswordText>
<web:UsernameToken>***</web:UsernameToken>
</soapenv:Header>
<soapenv:Body>
<cus:HQHPExpenseVoucherFlowServiceApprove_Input>
......................
</cus:HQHPExpenseVoucherFlowServiceApprove_Input>
</soapenv:Body>
</soapenv:Envelope>
调用webservice服务服务端添加了 header验证,项⽬⽤的是cxf,按照之前的思路,就是在客户端调⽤时直接添加,
在中添加hear 验证需要的内容,
中传递的内容是
<soapenv:Header>
<SessionType>***</web:SessionType>
<PasswordText>***</web:PasswordText>
<UsernameToken>***</web:UsernameToken>
</soapenv:Header>
调⽤客户端时就直接验证失败,错误错误就是soapenv:Envelope中添加需要添加⼀个xmlns:web="***/webservices" 的命名空间
问题到了,然后就就是漫长的查质料,怎么样在soapenv:Envelope中添加命名空间,⽹上居然搜不到解决⽅案.
最后发现可能思路有点局限了,⼀直都停留在cxf 的结局⽅案,经过漫长的测试下终于得到结局,贴下解决⽅案希望可以帮到更多的程序员朋友1,配置jaxws:handlers
(1)在webservice配置⽂件中添加jaxws:handlers(这个具体是什么东西百度⼀下这⾥不解释)
<jaxws:client id="client"  serviceClass="com.SpcService"
address="xxxxxx">
<!-- ⽤户名密码验证 -->
<jaxws:handlers>
<bean class="com.webService.Handler"></bean>
</jaxws:handlers>
</jaxws:client>
(2)xxxxHandler中的代码
package com.webService.client.handler;
import java.util.Set;
l.namespace.QName;
l.soap.SOAPEnvelope;
l.soap.SOAPException;
l.soap.SOAPHeader;
l.soap.SOAPHeaderElement;
l.soap.SOAPMessage;
l.ws.handler.MessageContext;
l.ws.handler.soap.SOAPHandler;
l.ws.handler.soap.SOAPMessageContext;
public class SiebelHeaderHandler implements SOAPHandler<SOAPMessageContext> {
public  SiebelHeaderHandler(){
System.out.println("crm,handler ⽤户验证加载成功........................");
}
public boolean handleMessage(SOAPMessageContext context) {
Boolean isRequest = (Boolean) (MessageContext.MESSAGE_OUTBOUND_PROPERTY);
QName usernameQName = new QName("xxx/webservices", "UsernameToken","siebelwsh");
QName passwordQName = new QName("xxx/webservices", "PasswordText","siebelwsh");
QName sessionTypeQName = new QName("xxx/webservices", "SessionType","siebelwsh");
if (isRequest) {
try {
SOAPMessage soapMsg = Message();
SOAPEnvelope soapEnv = SOAPPart().getEnvelope();
SOAPHeader soapHeader = Header();
soapEnv.addNamespaceDeclaration("web", "xxxxx/webservices");
//if no header, add one
if (soapHeader == null) {
soapHeader = soapEnv.addHeader();
}
String username = "xxx";
String password = "xxxx";
//添加SiebelUsernameToken
SOAPHeaderElement usernameHeaderElement = soapHeader.addHeaderElement(usernameQName);
usernameHeaderElement.addTextNode(username);
//添加SiebelPasswordText
SOAPHeaderElement passwordHeaderElement = soapHeader.addHeaderElement(passwordQName);
passwordHeaderElement.addTextNode(password);
//添加SiebelSessionType
SOAPHeaderElement SessionTypeHeaderElement = soapHeader.addHeaderElement(sessionTypeQName);                      SessionTypeHeaderElement.addTextNode("None");
soapMsg.saveChanges();
} catch (SOAPException e) {
//logger.Message());
} catch (Exception e) {
//(e.getMessage(),e);
}
}
return true;
}
public Set<QName> getHeaders() {
return null;
}
@Override
public boolean handleFault(SOAPMessageContext context) {
// TODO Auto-generated method stub
return false;
}
@Override
public void close(MessageContext context) {
// TODO Auto-generated method stub
}
}
添加Handler后顺利通过;
注意:其实这⾥<web:SessionType>***</web:SessionType> 的web可以相当于是⼀个变量 xmlns:web="***/webservices"

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。