Call调用webservice接口,使用命名空间和不使用命名空间的区别
Posted YOU记录
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Call调用webservice接口,使用命名空间和不使用命名空间的区别相关的知识,希望对你有一定的参考价值。
生活中我们会遇到许许多多的奇葩问题,而这些问题又是我们不得不解决的。
我先用一段代码来引出我想要说的内容:
import javax.jws.WebMethod; import javax.jws.WebService; /** * Created by 30721 on 2018/9/28. */ @WebService public interface WstwoInter { @WebMethod String getInfo(String xml); }
import cn.cy.webservice.inter.WstwoInter; import org.springframework.stereotype.Component; /** * Created by 30721 on 2018/9/28. */ @Component public class WstwoImpl implements WstwoInter { @Override public String getInfo(String xml) { System.out.println(xml); return xml; } }
而如果用org.apache.axis.client.Call类来调用就要这样写:
public static void main(String[] args) { try { String url = "http://127.0.0.1:8002/cxf/service/webtwo?wsdl"; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(url); QName qName = new QName("http://inter.webservice.cy.cn/", "getInfo"); call.setOperationName(qName);// WSDL里面描述的接口名称 QName qname = new QName("arg0"); call.addParameter(qname, XMLType.XSD_STRING, ParameterMode.IN);// 接口的参数 call.setReturnType(XMLType.XSD_STRING);// 设置返回类型 String temp = "测试人员"; String result = (String) call.invoke(new Object[] { temp }); // 给方法传递参数,并且调用方法 System.out.println("result is " + result); } catch (Exception e) { e.printStackTrace(); } }
描述wsdl中的方法需要带上命名空间(我这边的webservice的命名空间为:http://inter.webservice.cy.cn/)
如果使用则会报如下的错误:
org.apache.axis.AxisFault: Unexpected wrapper element getInfo found. Expected {http://inter.webservice.cy.cn/}getInfo. at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222) at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129) at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1776) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2964) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:504) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:327) at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227) at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696) at org.apache.axis.Message.getSOAPEnvelope(Message.java:435) at org.apache.axis.transport.http.HTTPSender.readFromSocket(HTTPSender.java:796) at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:144) at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32) at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118) at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83) at org.apache.axis.client.AxisClient.invoke(AxisClient.java:165) at org.apache.axis.client.Call.invokeEngine(Call.java:2784) at org.apache.axis.client.Call.invoke(Call.java:2767) at org.apache.axis.client.Call.invoke(Call.java:2443) at org.apache.axis.client.Call.invoke(Call.java:2366) at org.apache.axis.client.Call.invoke(Call.java:1812) at cn.cy.core.Main.main(Main.java:31) AxisFault faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Client faultSubcode: faultString: Unexpected wrapper element getInfo found. Expected {http://inter.webservice.cy.cn/}getInfo. faultActor: faultNode: faultDetail: {http://xml.apache.org/axis/}stackTrace:Unexpected wrapper element getInfo found. Expected {http://inter.webservice.cy.cn/}getInfo. at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222) at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129) at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1776) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2964) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:504) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:327) at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227) at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696) at org.apache.axis.Message.getSOAPEnvelope(Message.java:435) at org.apache.axis.transport.http.HTTPSender.readFromSocket(HTTPSender.java:796) at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:144) at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32) at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118) at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83) at org.apache.axis.client.AxisClient.invoke(AxisClient.java:165) at org.apache.axis.client.Call.invokeEngine(Call.java:2784) at org.apache.axis.client.Call.invoke(Call.java:2767) at org.apache.axis.client.Call.invoke(Call.java:2443) at org.apache.axis.client.Call.invoke(Call.java:2366) at org.apache.axis.client.Call.invoke(Call.java:1812) at cn.cy.core.Main.main(Main.java:31) {http://xml.apache.org/axis/}hostname:DESKTOP-MPUGA9U Unexpected wrapper element getInfo found. Expected {http://inter.webservice.cy.cn/}getInfo. at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222) at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129) at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1776) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2964) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:504) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:327) at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227) at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696) at org.apache.axis.Message.getSOAPEnvelope(Message.java:435) at org.apache.axis.transport.http.HTTPSender.readFromSocket(HTTPSender.java:796) at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:144) at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32) at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118) at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83) at org.apache.axis.client.AxisClient.invoke(AxisClient.java:165) at org.apache.axis.client.Call.invokeEngine(Call.java:2784) at org.apache.axis.client.Call.invoke(Call.java:2767) at org.apache.axis.client.Call.invoke(Call.java:2443) at org.apache.axis.client.Call.invoke(Call.java:2366) at org.apache.axis.client.Call.invoke(Call.java:1812) at cn.cy.core.Main.main(Main.java:31)
正常情况下接口交接都是服务端的服务写好了,然后客户端是直接根据写好的服务进行调用测试,然而我并非这样子的,与我交接的公司的接口平台是现成的,而且是老系统没有源码无法做修改。
这就很尴尬了,没办法只能服务端代码改了,不过工作量不大还好,他们发编译老系统的代码后给我发了一段这样的客户端代码:
String url = "http://127.0.0.1:8002/cxf/service/webtwo?wsdl"; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(url); QName qName = new QName("http://inter.webservice.cy.cn/", "getInfo"); call.setOperationName(qName);// WSDL里面描述的接口名称 QName qname = new QName("http://inter.webservice.cy.cn/", "arg0"); call.addParameter(qname, XMLType.XSD_STRING, ParameterMode.IN);// 接口的参数 call.setReturnType(XMLType.XSD_STRING);// 设置返回类型 String temp = "测试人员"; String result = (String) call.invoke(new Object[] { temp }); // 给方法传递参数,并且调用方法 System.out.println("result is " + result);
我看了一下代码发现与上面代码的区别就是参数那边也要带上命名空间地址。那么服务端的接口就需要这样改:
@WebService(targetNamespace="http://inter.webservice.cy.cn/") public interface WstwoInter { @WebMethod String getInfo(@WebParam(targetNamespace="http://inter.webservice.cy.cn/") String xml); }
这都是小问题,但是大问题出现了,服务发上去他们还是调用不到,发现还少了一个参数call.setUseSOAPAction(true);call.setSOAPActionURI(actionURI);这个参数者对应着@WebMethod(action=actionURI)
这个注释。代码:
@WebService(targetNamespace="http://inter.webservice.cy.cn/") public interface WstwoInter { @WebMethod(action="getInfo") String getInfo(@WebParam(targetNamespace="http://inter.webservice.cy.cn/") String xml); }
但是问题并非出在这个注释,而是出在老系统调用这个actionURI的时候竟然是带IP的,这样就让服务端的代码移植性变的非常差。测试环境和生产环境必然是两个不同的服务器不同的IP,不可能每次都修改代码上的注解
@WebMethod(action="http://ip/xxx")ip,不现实。这时他们又说了接口平台的另一个调用方式。不使用命名空间。代码如下:
public static void main(String[] args) { try { String url = "http://127.0.0.1:8002/cxf/service/webtwo?wsdl"; Service service = new Service(); Call call = (Call) service.createCall(); call.setUseSOAPAction(true); call.setTargetEndpointAddress(url); QName qName = new QName("getInfo"); call.setOperationName(qName);// WSDL里面描述的接口名称 QName qname = new QName("arg0"); call.addParameter(qname, XMLType.XSD_STRING, ParameterMode.IN);// 接口的参数 call.setSOAPActionURI("getInfo"); call.setReturnType(XMLType.XSD_STRING);// 设置返回类型 String temp = "测试人员"; String result = (String) call.invoke(new Object[] { temp }); // 给方法传递参数,并且调用方法 System.out.println("result is " + result); } catch (Exception e) { e.printStackTrace(); } }
这段代码少了命名空间,多了一个actionURI,但是这个URI跟上面的不同,这个URI带的是方法名,没办法,他们改不了,只能我们服务端来改了。我这样子改:
@WebService @SOAPBinding(style = SOAPBinding.Style.RPC, use = SOAPBinding.Use.ENCODED) public interface WstwoInter { @WebMethod(action="getInfo") String getInfo(String xml); }
终于接口跟他们调通了。
如果想深入理解@SOAPBinding这个注解,可参考https://blog.csdn.net/comman1999/article/details/1967907。
以上是关于Call调用webservice接口,使用命名空间和不使用命名空间的区别的主要内容,如果未能解决你的问题,请参考以下文章