如何设置Axis2 命名空间

qq_25944641 2017-07-15 05:10:00
通过axis2自动生成客户端,但生成的xml如下:
<?xml version="1.0" encoding="utf-8"?>

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
....
....
</soapenv:Body>
</soapenv:Envelope>
服务端需要的xml格式为
<?xml version="1.0" encoding="utf-8"?>

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="un-005056836C731EE68A92374814F9BEBF">
<wsse:Username>admin</wsse:Username>
<wsse:Password>password</wsse:Password>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">La/cpG/k9SoUnjwl1E/ZRQ==</wsse:Nonce>
<wsu:Created>2016-06-02T07:15:04Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Body>
</soapenv:Envelope>

百度了好久,终于生成了添加上token 认证了
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<wsse:Security
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="un-005056836C731EE68A92374814F9BEBF">
<wsse:Username>admin</wsse:Username>
<wsse:Password>password</wsse:Password>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">La/cpG/k9SoUnjwl1E/ZRQ==</wsse:Nonce>
<wsu:Created
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">2016-06-02T07:15:04Z
</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
,,,
,
</soapenv:Body>
</soapenv:Envelope>

但现在唯一的问题是<soap-env:Header >没有命名空间,不知道如何添加命名空间,让他成为服务端需要的那样
<soap-env:Header xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
百度了好久没结果啊!
...全文
647 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_25944641 2017-07-26
  • 打赏
  • 举报
回复
总算自己弄出来了,看来还是要多看官方文档啊! http://ws.apache.org/axiom/apidocs/index.html
	    //add <soap-env:Header> nameSpace
        OMNamespace headerNS=omFactory.createOMNamespace(
        		"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd","wsu");  
        env.getHeader().declareNamespace(headerNS);
        
        _serviceClient.addHeadersToEnvelope(env);
主要解决方法就是找到env.getHeader().declareNamespace这个api,然后设置命名空间就可以了。 总结:有时候开源框架并不能很好的满足我们的项目开发,需要做些扩展时,要学会看官网文档api,这样会少走一些弯路!
qq_25944641 2017-07-15
  • 打赏
  • 举报
回复
添加Axis2中添加SOAP头的代码为
	private void addSapSecurityHeaders(org.apache.axis2.client.ServiceClient _serviceClient){  
	    OMFactory omFactory = OMAbstractFactory.getOMFactory();  
	    OMNamespace omNS=omFactory.createOMNamespace(
	    		"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd","wsse");  
	    

	    OMElement head = omFactory.createOMElement("Security", omNS);  
	    OMElement token = omFactory.createOMElement("UsernameToken", omNS);
	    token.addAttribute(omFactory.createOMAttribute("wsu:Id", null, "un-005056836C731EE68A92374814F9BEBF"));
	    head.addChild(token);  
	    
	          
	    OMElement userName = omFactory.createOMElement("Username", omNS);  
	    userName.addChild(omFactory.createOMText(userName, "admin"));  
	    token.addChild(userName);  
	          
	    OMElement password = omFactory.createOMElement("Password", omNS);  
	    password.addChild(omFactory.createOMText(password, "password"));  
	    token.addChild(password);
	    
	    OMElement nonce = omFactory.createOMElement("Nonce", omNS);
	    nonce.addAttribute(omFactory.createOMAttribute("EncodingType", null, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"));  
	    nonce.addChild(omFactory.createOMText(nonce, "La/cpG/k9SoUnjwl1E/ZRQ=="));  
	    token.addChild(nonce);
	    
	    OMElement created = omFactory.createOMElement("Created", 
	    		"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd","wsu");
	    created.addChild(omFactory.createOMText(created, "2016-06-02T07:15:04Z"));  
	    token.addChild(created);
	    
	    //CallerInformation
        String AUTH_NS = "http://www.sap.com/webas/712/soap/features/runtime/metering/";
        OMElement callerInformation = omFactory.createOMElement("CallerInformation", AUTH_NS,"n0");
        callerInformation.addAttribute(omFactory.createOMAttribute("wsu:Id", null, "part-CallerInformation-4"));

        OMElement type = omFactory.createOMElement("Type", AUTH_NS,"m");
        type.addChild(omFactory.createOMText(type, "SA"));
        OMElement company = omFactory.createOMElement("Company", AUTH_NS,"m");
        company.addChild(omFactory.createOMText(company, "0020698959"));
        OMElement sys = omFactory.createOMElement("Sys", AUTH_NS,"m");
        sys.addChild(omFactory.createOMText(sys, "QPM_110"));
        callerInformation.addChild(type);  
        callerInformation.addChild(company);
        callerInformation.addChild(sys); 
        
        
	    _serviceClient.addHeader(head);
	    _serviceClient.addHeader(callerInformation);

	}
,但就是不知道怎么设置顶级 <soapenv:Header/>命名空间啊

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧