WebService小白的使用记录
webservice客户端开发遇见的问题
本人第一次使用webService,只希望能帮助其他第一次使用的人,大神勿喷
我采用的是下面第二种方式,eclipse那种不知道应该怎么携带认证信息。
-
如何携带表头的认证信息
如果出现如下报错,说明没有认证信息
解决方式:添加如下代码
org.apache.axis.message.SOAPHeaderElement soapHeader = new SOAPHeaderElement("http://tempuri.org/","CredentialSoapHeader"); soapHeader.setNamespaceURI("http://tempuri.org/"); soapHeader.addChildElement("UserId").setValue(userName); soapHeader.addChildElement("Password").setValue(passWord); call.addHeader(soapHeader); //需要注意的是参数CredentialSoapHeader UserId Password要和文档中给的对应。
-
SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
这个意思是解析xml遇到一个子节点处理不了
所以我用文档接数据解决方式:添加如下代码
call.setReturnType(XMLType.SOAP_DOCUMENT); Document ret = (Document)call.invoke("http://tempuri.org/", "GetLibraryByIds", new Object[]{isbn}); //这里的参数是namespace 接口名 参数
-
客户端完成如何得到测试报文数据
解决方式:添加如下代码
finally { //请求报文数据 Message requestMessage = call.getMessageContext().getRequestMessage(); //响应报文数据 Message responseMessage = call.getMessageContext().getResponseMessage(); if(null != requestMessage) { String reqXml = requestMessage.getSOAPPartAsString(); System.out.println(reqXml); System.out.println("==================>"); } if(null != requestMessage) { tring respXml = responseMessage.getSOAPPartAsString(); System.out.println(respXml); }
-
如何将wsdl转化为本地代码
一共有两种方式- 一种是eclipse的方式
- Eclipse中,file -> 选择 New –> Other…
- 选择web Service Client
- 输入WSDL路径,点击finish
- 本地方法调用就可以了
=======================================================
- 第二种采用wsdl2java工具生成webservice客户端代码
- 下载Apache-cxf发布包,地址:http://cxf.apache.org/download.html
- 解压
- 配置环境变量 CXF_HOME 你自己的地址(类似JAVA_HOME)
- Path中修改 %CXF_HOME%\bin
- 打开cmd 输入命令 wsdl2java –help 出现如下配置成功
- 输入命令 wsdl2java –encoding utf-8 –d 你要存储在项目的路径 wsdl路径
- 如果出现这个问题
需要修改jdk文件,在这个目录下新建文件jaxp.properties
添加
再次执行cmd命令,没有报错,文件添加成功
-
附上我的maven坐标 和 导入的包
有用没用的都在里面了
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery</artifactId> <version>0.5</version> </dependency> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis-jaxrpc</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis-saaj</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis-ant</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>axis</groupId> <artifactId>axis-wsdl4j</artifactId> <version>1.5.1</version> </dependency> <dependency> <groupId>javax.xml.soap</groupId> <artifactId>saaj-api</artifactId> <version>1.3.5</version> </dependency> <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.12</version> </dependency> <dependency> <groupId>opensymphony</groupId> <artifactId>sitemesh</artifactId> <version>2.4.2</version> </dependency> <dependency> <groupId>javax.xml.rpc</groupId> <artifactId>javax.xml.rpc-api</artifactId> <version>1.1.1</version> </dependency> <!-- Apache CXF Dependencies --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.13</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.7.13</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>2.7.13</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>2.7.13</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-wsdl</artifactId> <version>3.1.4</version> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency> </dependencies>
我的导包信息
import java.net.MalformedURLException; import java.rmi.RemoteException; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceException; import javax.xml.soap.SOAPException; import org.apache.axis.AxisFault; import org.apache.axis.Message; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; import org.apache.axis.message.SOAPHeaderElement; import org.w3c.dom.Document;
学习参考:
https://blog.csdn.net/woshisangsang/article/details/89637171
https://blog.csdn.net/qq_33207383/article/details/99305720?utm_medium=distribute.pc_relevant.none-task-blog-2
https://blog.csdn.net/smilejuan/article/details/82769782#comments
本文来自博客园,作者:衍君的不拿拿,转载请注明原文链接:https://www.cnblogs.com/lyjbanana/p/15818759.html