Java Webservice WSDL加载但不是webservice

时间:2017-03-08 19:42:08

标签: java web-services soap wsdl localhost

我正在尝试学习如何使用Java创建自己的Web服务以供我自己理解。

当我到localhost:9998 / calculate?wsdl我可以看到我的wsdl文件,但是当我到localhost时:9998 /计算我看不到我的web服务。我刚刚在chrome中遇到错误,说ERR_EMPTY_RESPONSE localhost没有发送任何数据。

这是我的界面:

correct == 0

以下是我对界面的实现:

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);
    }
}

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

您的网络服务是正确的。您需要发送HTTP POST请求。

选项1

您可以使用soapUI(https://www.soapui.org/downloads/soapui.html)对其进行测试。使用http://localhost:9998/calculate?WSDL

创建新项目

enter image description here

enter image description here

选项2

您也可以使用curl

从命令行测试它

创建soap请求。我在/var/tmp/calculate_add.xml上创建了它:

me@centos> pwd
/var/tmp
me@centos> 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:

me@centos> 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>

答案 1 :(得分:0)

您无法通过简单的浏览器测试网络服务。您必须编写Web服务客户端代码或需要使用某些工具。邮差是这样的工具之一,可用于测试SOAP,REST&amp;任何http服务。

http://blog.getpostman.com/2014/08/22/making-soap-requests-using-postman/

相关问题