2017-03-08 81 views
1

我想了解如何使用Java自己理解創建自己的Web服務。Java Web服務WSDL加載但不是web服務

當我到localhost:9998/calculate?wsdl我可以看到我的wsdl文件,但是當我到localhost:9998 /計算我看不到我的web服務。我只是在chrome中出現錯誤,說ERR_EMPTY_RESPONSE localhost沒有發送任何數據。

這裏是我的接口:

package Webservice; 


import javax.jws.WebMethod; 
import javax.jws.WebService; 

//Service Endpoint Interface 
@WebService 
public interface Calculate{ 

    @WebMethod 
    public int add(int x, int y); 

    @WebMethod 
    public int sub(int x, int y); 

    @WebMethod 
    public int mul(int x, int y); 
} 

這裏是我的接口的實現:

package Webservice; 
import javax.jws.WebService; 

//Service Implementation 
@WebService(endpointInterface = "Webservice.Calculate") 
public class CalculateImpl implements Calculate { 

    public CalculateImpl() { 

    } 

    @Override 
    public int add(int x, int y) { 
     return (x+y); 
    } 

    @Override 
    public int sub(int x, int y) { 
     return (x-y); 
    } 

    @Override 
    public int mul(int x, int y) { 
     return (x*y); 
    } 
} 

,這裏是我的出版商:

package Webservice; 
import javax.xml.ws.Endpoint; 

public class CalculatePublisher { 
    public static void main(String[] args) { 
     Endpoint ep = Endpoint.create(new CalculateImpl()); 
     ep.publish("http://localhost:9998/calculate"); 
    } 
} 

任何幫助,將不勝感激。

+0

WSDL表示SOAP服務。每個服務操作都要求HTTP請求是一個SOAP調用,這與通過在瀏覽器中輸入URL所做的普通HTTP GET請求不同。請參閱https://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383526和https://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383539。 – VGR

回答

0

您的網絡服務是正確的。您需要發送HTTP POST請求。

選項1

你可以用的soapUI(https://www.soapui.org/downloads/soapui.html)進行測試。創建使用curl

創建SOAP請求,利用http://localhost:9998/calculate?WSDL

enter image description here

enter image description here

選項2

您也可以通過命令行測試一個新的項目。我在/var/tmp/calculate_add.xml中創建了它:

[email protected]> pwd 
/var/tmp 
[email protected]> cat calculate_add.xml 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <web:add> 
     <arg0>1</arg0> 
     <arg1>1</arg1> 
     </web:add> 
    </soapenv:Body> 
</soapenv:Envelope> 

發送請求。 注:改變字符串YourServerIP到你的真實IP:

[email protected]> curl -i -H "Content-Type: text/xml;charset=UTF-8" --data "@/var/tmp/calculate_add.xml" -X POST http://YourServerIP:9998/calculate 

的迴應是:

HTTP/1.1 200 OK 
Date: Thu, 09 Mar 2017 08:55:12 GMT 
Transfer-encoding: chunked 
Content-type: text/xml; charset=utf-8 

<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:addResponse xmlns:ns2="http://Webservice/"><return>2</return></ns2:addResponse></S:Body></S:Envelope> 
+0

好吧,讓我們說我想要一個頁面是localhost:9998/myhtmlpage或沿着這些線條的東西,這是一個帶有兩個文本框的網頁,這些文本框在我的Web服務上調用了add方法。有沒有辦法在java/eclipse中做到這一點?我可能需要一些javascript在我的html頁面的幕後。 –