WSDL WebService的创建和使用实例

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


WebServices Framework要选JAX-WS:


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

package com.webservice;

public class WebService{
	
	public String printData(String printerName){
		String strRet = "Welcome to use WebService, " + printerName;
		
		System.out.println("Print from WebService:" + strRet);
		
		return strRet;
	}	
}

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内容的
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3-hudson-390-. -->
<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/">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://webservice.com/" schemaLocation="WebServiceService_schema1.xsd"/>
    </xsd:schema>
  </types>
  <message name="printData">
    <part element="tns:printData" name="parameters"/>
  </message>
  <message name="printDataResponse">
    <part element="tns:printDataResponse" name="parameters"/>
  </message>
  <portType name="WebServiceDelegate">
    <operation name="printData">
      <input message="tns:printData"/>
      <output message="tns:printDataResponse"/>
    </operation>
  </portType>
  <binding name="WebServicePortBinding" type="tns:WebServiceDelegate">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="printData">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="WebServiceService">
    <port binding="tns:WebServicePortBinding" name="WebServicePort">
      <soap:address location="http://localhost:8080/WebService/WebServicePort"/>
    </port>
  </service>
</definitions>

WebServiceService_schema1.xsd:用来说明Web Service的命令及其参数
比如:sample里面的WebService是【printData】,有一个String类型的参数【arg0】,返回值一个String类型的值。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://webservice.com/" targetNamespace="http://webservice.com/" version="1.0">

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

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

  <xs:complexType name="printData">
    <xs:sequence>
      <xs:element minOccurs="0" name="arg0" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="printDataResponse">
    <xs:sequence>
      <xs:element minOccurs="0" name="return" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</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】


代码如下:
package com.webservice;

public class WebServiceTest{
	
	public static void main(String[] args){
		WebServiceService wssPrintData = new WebServiceService();
		WebServiceDelegate wsdPrintData = wssPrintData.getWebServicePort();
		
		System.out.println(wsdPrintData.printData("sun"));
	}	
}

6.【WebServiceTest.java】右键→Run As→Java Application
输出结果:
Welcome to use WebService, sun


方法2:用HttpClient调用:
package com.httpclientforwsdl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;

public class WebServiceHttpClientTest{

	public synchronized static String accessService(String wsdl,String ns,String method,Map<String,String> params,String result)throws Exception{  
        //拼接参数  
        String param = getParam(params);  
        String soapResponseData = "";  
        //拼接SOAP  
        StringBuffer soapRequestData = new StringBuffer("");  
        soapRequestData.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");  
        soapRequestData.append("<soap:Body>");  
        soapRequestData.append("<ns1:"+method+" xmlns:ns1=\""+ns+"\">");  
        soapRequestData.append(param);  
        soapRequestData.append("</ns1:"+method+">");  
        soapRequestData.append("</soap:Body>" + "</soap:Envelope>");  
        PostMethod postMethod = new PostMethod(wsdl);  
        // 然后把Soap请求数据添加到PostMethod中  
        byte[] b=null;  
        InputStream is=null;  
        try {  
            b = soapRequestData.toString().getBytes("utf-8");   
            is = new ByteArrayInputStream(b, 0, b.length);  
            RequestEntity re = new InputStreamRequestEntity(is, b.length,"text/xml; charset=UTF-8");  
            postMethod.setRequestEntity(re);  
            HttpClient httpClient = new HttpClient();  
            int status = httpClient.executeMethod(postMethod);  
            System.out.println("status:"+status);  
            if(status==200){  
                soapResponseData = getMesage(postMethod.getResponseBodyAsString(),result);  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally{  
            if(is!=null){  
                is.close();  
            }  
        }  
        return soapResponseData;  
    }  
      
    public static String getParam(Map<String,String> params){  
        String param = "";  
        if(params!=null){  
            Iterator<String> it  = params.keySet().iterator();  
            while(it.hasNext()){  
                String str = it.next();  
                param+="<"+str+">";  
                param+=params.get(str);  
                param+="</"+str+">";  
            }  
        }  
        return param;  
    }  
      
    public static String getMesage(String soapAttachment,String result){  
        System.out.println("message:"+soapAttachment);  
        if(result==null){  
            return null;  
        }  
        if(soapAttachment!=null && soapAttachment.length()>0){  
            int begin = soapAttachment.indexOf(result);  
            begin = soapAttachment.indexOf(">", begin);  
            int end = soapAttachment.indexOf("</"+result+">");  
            String str = soapAttachment.substring(begin+1, end);  
            str = str.replaceAll("<", "<");  
            str = str.replaceAll(">", ">");  
            return str;  
        }else{  
            return "";  
        }  
    }  
      
    /** 
     * @param args 
     */  
    public static void main(String[] args) {   
        try {  
            Map<String,String> param = new HashMap<String,String>();  
            param.put("arg0", "sun");
            String wsdl="http://localhost:8080/WebService/WebServicePort?wsdl";  
            String ns = "http://webservice.com/";  
            String method="printData";  
            String response =accessService(wsdl,ns,method,param,"return");  
            System.out.println("main:"+response);  
              
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}

显示结果:
status:200
七月 15, 2016 3:43:27 下午 org.apache.commons.httpclient.HttpMethodBase getResponseBody
警告: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
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>
main:Welcome to use WebService, sun


相关文章:

WSDL WebService和RestFul WebService的个人理解:
http://blog.csdn.net/sunroyi666/article/details/51939802


RestFul WebService的创建和使用实例:

http://blog.csdn.net/sunroyi666/article/details/51918675

WSDL WebService和RestFul WebService的Sample代码:
http://download.csdn.net/detail/sunroyi666/9577143

  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,可以看出这是一个关于使用Spring Boot创建基于WSDLWebService的示例代码。在这个示例中,通过访问"http://localhost:8080/webservice/api?wsdl"来创建一个客户端,并调用getUser方法来获取用户信息。同时,还提供了WebServiceServer接口的定义,其中包含了invoke方法的参数定义。另外,还提供了MonthPlanWS接口的定义,其中包含了getMonthPlan方法的参数定义。这些接口定义了WebService的操作和参数。 #### 引用[.reference_title] - *1* [SpringBoot集成webservice](https://blog.csdn.net/qq_42582773/article/details/127917067)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [SpringBoot项目添加WebService服务](https://blog.csdn.net/weixin_43493089/article/details/125523527)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [springboot集成webservice](https://blog.csdn.net/bbj12345678/article/details/130217670)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值