本文介绍了如何使用Apache Axis2和WSDL2Java向SOAP响应添加命名空间引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在查看我正在开发的Web服务的SOAP输出,我发现了一些好奇的东西:I'm looking at the SOAP output from a web service I'm developing, and I noticed something curious:<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> <soapenv:Body> <ns1:CreateEntityTypesResponse xmlns:ns1="http://somedomain.com/wsinterface"> <newKeys> <value>1234</value> </newKeys> <newKeys> <value>2345</value> </newKeys> <newKeys> <value>3456</value> </newKeys> <newKeys xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> <newKeys xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> <errors>Error1</errors> <errors>Error2</errors> </ns1:CreateEntityTypesResponse> </soapenv:Body></soapenv:Envelope>我有两个newKeys元素是nil,两个元素都插入了xsi的命名空间引用。我想在soapenv:Envelope元素中包含该命名空间,以便命名空间引用只发送一次。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.我使用WSDL2Java生成服务框架,所以我无法直接访问Axis2 API。I am using WSDL2Java to generate the service skeleton, so I don't directly have access to the Axis2 API.推荐答案 使用WSDL2Java 如果您已经使用过Axis2 WSDL2Java工具,那么您可能会遇到它为您生成的内容。但是,您可以尝试更改此部分中的框架:Using WSDL2JavaIf 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-codedIf 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... 在aar中公开服务 如果要在aar中创建服务,则可能会影响使用目标命名空间或模式命名空间属性生成的SOAP消息(请参阅这篇文章)。希望有所帮助。 这篇关于如何使用Apache Axis2和WSDL2Java向SOAP响应添加命名空间引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-15 23:38