简体   繁体   English

Java Webservice WSDL加载但不是webservice

[英]Java Webservice WSDL loading but not webservice

I'm trying to learn how to create my own web service in Java for my own understanding. 我正在尝试学习如何使用Java创建自己的Web服务以供我自己理解。

When I got to localhost:9998/calculate?wsdl I can see my wsdl file, but when I got to localhost:9998/calculate I can't see my web service. 当我到localhost:9998 / calculate?wsdl我可以看到我的wsdl文件,但当我到localhost:9998 /计算我看不到我的网络服务。 I just get an error in chrome that says ERR_EMPTY_RESPONSE localhost didn't send any data. 我刚刚在chrome中遇到错误,说ERR_EMPTY_RESPONSE localhost没有发送任何数据。

Here is my interface: 这是我的界面:

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

Here is my implementation of the interface: 这是我的界面实现:

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

and here is my publisher: 这是我的出版商:

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

any help would be appreciated. 任何帮助,将不胜感激。

Your web service is correct. 您的网络服务是正确的。 You need to send a HTTP POST request. 您需要发送HTTP POST请求。

Option 1 选项1

You could test it with soapUI ( https://www.soapui.org/downloads/soapui.html ). 您可以使用soapUI( https://www.soapui.org/downloads/soapui.html )对其进行测试。 Create a new project using http://localhost:9998/calculate?WSDL 使用http:// localhost:9998 / calculate?WSDL创建一个新项目

在此输入图像描述

在此输入图像描述

Option 2 选项2

You could also test it from the command line using curl 您还可以使用curl从命令行测试它

Create the soap request. 创建soap请求。 I created it on /var/tmp/calculate_add.xml: 我在/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>

Send the request. 发送请求。 Note: Change the String YourServerIP to your real IP: 注意:将String 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

The response is: 回应是:

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>

You can't test webservice thru simple browser. 您无法通过简单的浏览器测试Web服务。 Either you have to write webservice client code or need to use some tool. 您必须编写Web服务客户端代码或需要使用某些工具。 Postman is one of such tool can be used to test SOAP,REST & any http services. 邮差是这样的工具之一,可用于测试SOAP,REST和任何http服务。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM