2017-03-08 120 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页面的幕后。 –