WSDL WebService的创建和使用实例

本文介绍如何使用JAX-WS框架创建WebService,并通过两种方式调用该服务:一是利用WebServiceClient进行调用;二是使用HttpClient发送SOAP请求进行调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:https://blog.youkuaiyun.com/sunroyi666/article/details/51917991

一. WSDL WebService的创建:
1.创建【Web Service Project】:


WebServices Framework要选JAX-WS:


2.写一个简单的测试用例:

 
  1. package com.webservice;

  2.  
  3. public class WebService{

  4.  
  5. public String printData(String printerName){

  6. String strRet = "Welcome to use WebService, " + printerName;

  7.  
  8. System.out.println("Print from WebService:" + strRet);

  9.  
  10. return strRet;

  11. }

  12. }


3.发布Web Service:
点击工具栏的New Web Service:


Strategy选择第二个(Create web service from Java class):


勾选【Generate WSDL in project】:


点击【Finish】后,系统会在WEB-INF/wsdl下生成两个文件:


WebServiceService.wsdl:这个文件是用来描述Web Service内容的

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3-hudson-390-. -->

  3. <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice.com/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="WebServiceService" targetNamespace="http://webservice.com/">

  4. <types>

  5. <xsd:schema>

  6. <xsd:import namespace="http://webservice.com/" schemaLocation="WebServiceService_schema1.xsd"/>

  7. </xsd:schema>

  8. </types>

  9. <message name="printData">

  10. <part element="tns:printData" name="parameters"/>

  11. </message>

  12. <message name="printDataResponse">

  13. <part element="tns:printDataResponse" name="parameters"/>

  14. </message>

  15. <portType name="WebServiceDelegate">

  16. <operation name="printData">

  17. <input message="tns:printData"/>

  18. <output message="tns:printDataResponse"/>

  19. </operation>

  20. </portType>

  21. <binding name="WebServicePortBinding" type="tns:WebServiceDelegate">

  22. <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

  23. <operation name="printData">

  24. <soap:operation soapAction=""/>

  25. <input>

  26. <soap:body use="literal"/>

  27. </input>

  28. <output>

  29. <soap:body use="literal"/>

  30. </output>

  31. </operation>

  32. </binding>

  33. <service name="WebServiceService">

  34. <port binding="tns:WebServicePortBinding" name="WebServicePort">

  35. <soap:address location="http://localhost:8080/WebService/WebServicePort"/>

  36. </port>

  37. </service>

  38. </definitions>


WebServiceService_schema1.xsd:用来说明Web Service的命令及其参数
比如:sample里面的WebService是【printData】,有一个String类型的参数【arg0】,返回值一个String类型的值。

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://webservice.com/" targetNamespace="http://webservice.com/" version="1.0">

  3.  
  4. <xs:element name="printData" type="tns:printData"/>

  5.  
  6. <xs:element name="printDataResponse" type="tns:printDataResponse"/>

  7.  
  8. <xs:complexType name="printData">

  9. <xs:sequence>

  10. <xs:element minOccurs="0" name="arg0" type="xs:string"/>

  11. </xs:sequence>

  12. </xs:complexType>

  13.  
  14. <xs:complexType name="printDataResponse">

  15. <xs:sequence>

  16. <xs:element minOccurs="0" name="return" type="xs:string"/>

  17. </xs:sequence>

  18. </xs:complexType>

  19. </xs:schema>


将WebService项目部署到Tomcat即可。
(部署方法略)

二. WSDL WebService的调用:
方法1:创建Web Service Client来调用:

1.创建【Java Project】:


2.点击工具栏的New Web Service Client:




3.选择【WSDL URL】:


4.点击【Next】完成创建后,在src/com/webservice下,自动生成相关文件。(WebServiceTest.java除外,这个是自己创建的调用文件)


5.创建【WebServiceTest.java】


代码如下:

 
  1. package com.webservice;

  2.  
  3. public class WebServiceTest{

  4.  
  5. public static void main(String[] args){

  6. WebServiceService wssPrintData = new WebServiceService();

  7. WebServiceDelegate wsdPrintData = wssPrintData.getWebServicePort();

  8.  
  9. System.out.println(wsdPrintData.printData("sun"));

  10. }

  11. }


6.【WebServiceTest.java】右键→Run As→Java Application
输出结果:

Welcome to use WebService, sun



方法2:用HttpClient调用:

 
  1. package com.httpclientforwsdl;

  2.  
  3. import java.io.ByteArrayInputStream;

  4. import java.io.InputStream;

  5. import java.util.HashMap;

  6. import java.util.Iterator;

  7. import java.util.Map;

  8.  
  9. import org.apache.commons.httpclient.HttpClient;

  10. import org.apache.commons.httpclient.methods.InputStreamRequestEntity;

  11. import org.apache.commons.httpclient.methods.PostMethod;

  12. import org.apache.commons.httpclient.methods.RequestEntity;

  13.  
  14. public class WebServiceHttpClientTest{

  15.  
  16. public synchronized static String accessService(String wsdl,String ns,String method,Map<String,String> params,String result)throws Exception{

  17. //拼接参数

  18. String param = getParam(params);

  19. String soapResponseData = "";

  20. //拼接SOAP

  21. StringBuffer soapRequestData = new StringBuffer("");

  22. soapRequestData.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");

  23. soapRequestData.append("<soap:Body>");

  24. soapRequestData.append("<ns1:"+method+" xmlns:ns1=\""+ns+"\">");

  25. soapRequestData.append(param);

  26. soapRequestData.append("</ns1:"+method+">");

  27. soapRequestData.append("</soap:Body>" + "</soap:Envelope>");

  28. PostMethod postMethod = new PostMethod(wsdl);

  29. // 然后把Soap请求数据添加到PostMethod中

  30. byte[] b=null;

  31. InputStream is=null;

  32. try {

  33. b = soapRequestData.toString().getBytes("utf-8");

  34. is = new ByteArrayInputStream(b, 0, b.length);

  35. RequestEntity re = new InputStreamRequestEntity(is, b.length,"text/xml; charset=UTF-8");

  36. postMethod.setRequestEntity(re);

  37. HttpClient httpClient = new HttpClient();

  38. int status = httpClient.executeMethod(postMethod);

  39. System.out.println("status:"+status);

  40. if(status==200){

  41. soapResponseData = getMesage(postMethod.getResponseBodyAsString(),result);

  42. }

  43. } catch (Exception e) {

  44. e.printStackTrace();

  45. } finally{

  46. if(is!=null){

  47. is.close();

  48. }

  49. }

  50. return soapResponseData;

  51. }

  52.  
  53. public static String getParam(Map<String,String> params){

  54. String param = "";

  55. if(params!=null){

  56. Iterator<String> it = params.keySet().iterator();

  57. while(it.hasNext()){

  58. String str = it.next();

  59. param+="<"+str+">";

  60. param+=params.get(str);

  61. param+="</"+str+">";

  62. }

  63. }

  64. return param;

  65. }

  66.  
  67. public static String getMesage(String soapAttachment,String result){

  68. System.out.println("message:"+soapAttachment);

  69. if(result==null){

  70. return null;

  71. }

  72. if(soapAttachment!=null && soapAttachment.length()>0){

  73. int begin = soapAttachment.indexOf(result);

  74. begin = soapAttachment.indexOf(">", begin);

  75. int end = soapAttachment.indexOf("</"+result+">");

  76. String str = soapAttachment.substring(begin+1, end);

  77. str = str.replaceAll("<", "<");

  78. str = str.replaceAll(">", ">");

  79. return str;

  80. }else{

  81. return "";

  82. }

  83. }

  84.  
  85. /**

  86. * @param args

  87. */

  88. public static void main(String[] args) {

  89. try {

  90. Map<String,String> param = new HashMap<String,String>();

  91. param.put("arg0", "sun");

  92. String wsdl="http://localhost:8080/WebService/WebServicePort?wsdl";

  93. String ns = "http://webservice.com/";

  94. String method="printData";

  95. String response =accessService(wsdl,ns,method,param,"return");

  96. System.out.println("main:"+response);

  97.  
  98. } catch (Exception e) {

  99. e.printStackTrace();

  100. }

  101. }

  102. }


显示结果:

 
  1. status:200

  2. 七月 15, 2016 3:43:27 下午 org.apache.commons.httpclient.HttpMethodBase getResponseBody

  3. 警告: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

  4. message:<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:printDataResponse xmlns:ns2="http://webservice.com/"><return>Welcome to use WebService, sun</return></ns2:printDataResponse></S:Body></S:Envelope>

  5. main:Welcome to use WebService, sun

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值