java soap namespace,如何使用Apache Axis2和WSDL2Java向SOAP响应添加命名空间引用

这篇博客探讨了如何在使用WSDL2Java生成的服务骨架中,通过修改SOAP消息来避免重复的命名空间引用。作者提供了两种解决方案:一是通过修改生成的骨架代码,直接在SOAP Envelope元素中声明命名空间;二是手动编码服务时如何实现这一目标。此外,还提到了在aar中创建服务时可能影响SOAP消息的方法。
摘要由CSDN通过智能技术生成

I'm looking at the SOAP output from a web service I'm developing, and I noticed something curious:

1234

2345

3456

Error1

Error2

I have two newKeys elements that are nil, and both elements insert a namespace reference for xsi. I'd like to include that namespace in the soapenv:Envelope element so that the namespace reference is only sent once.

I am using WSDL2Java to generate the service skeleton, so I don't directly have access to the Axis2 API.

解决方案

Using WSDL2Java

If you have used the Axis2 WSDL2Java tool you're kind of stuck with what it generates for you. However you can try to change the skeleton in this section:

// create SOAP envelope with that payload

org.apache.axiom.soap.SOAPEnvelope env = null;

env = toEnvelope(

getFactory(_operationClient.getOptions().getSoapVersionURI()),

methodName,

optimizeContent(new javax.xml.namespace.QName

("http://tempuri.org/","methodName")));

//adding SOAP soap_headers

_serviceClient.addHeadersToEnvelope(env);

To add the namespace to the envelope add these lines somewhere in there:

OMNamespace xsi = getFactory(_operationClient.getOptions().getSoapVersionURI()).

createOMNamespace("http://www.w3.org/2001/XMLSchema-instance", "xsi");

env.declareNamespace(xsi);

Hand-coded

If you are "hand-coding" the service you might do something like this:

SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();

SOAPEnvelope envelope = fac.getDefaultEnvelope();

OMNamespace xsi = fac.createOMNamespace("http://www.w3.org/2001/XMLSchema-instance", "xsi");

envelope.declareNamespace(xsi);

OMNamespace methodNs = fac.createOMNamespace("http://somedomain.com/wsinterface", "ns1");

OMElement method = fac.createOMElement("CreateEntityTypesResponse", methodNs);

//add the newkeys and errors as OMElements here...

Exposing service in aar

If you are creating a service inside an aar you may be able to influence the SOAP message produced by using the target namespace or schema namespace properties (see this article).

Hope that helps.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值