本文講解如何快速發佈一個簡單的webservice服務端,在實際工作中一般會使用cxf、axis等框架來實現
一、編寫Java代碼
package com.wsdl.services;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
/**
* @author administrator
* @date 2020-06-09 22:02
*/
@WebService
public class HelloWorld {
@WebMethod
public String sayHello(String str) {
String result = "Hello World " + str;
return result;
}
public static void main(String[] args) {
String wsdlAddress = "http://127.0.0.1:8001/HelloWorld";
HelloWorld implementor = new HelloWorld();
Endpoint.publish(wsdlAddress, implementor);
System.out.println("server is running");
}
}
二、運行結果
http://127.0.0.1:8001/HelloWorld?wsdl
三、測試結果
需要提前安裝好SoapUI工具,本文使用SoapUI工具驗證服務
- 測試請求報文
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.wsdl.com/">
<soapenv:Header/>
<soapenv:Body>
<ser:sayHello>
<!--Optional:-->
<arg0><![CDATA[張三丰 ]]></arg0>
</ser:sayHello>
</soapenv:Body>
</soapenv:Envelope>