[英]Java SE 8: Minimal JSON webservice
我试图在Java 8中制作一个最小的JSON Web服务。这就是我试图做到的方式:
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class DemoHttpServer {
@WebMethod
public double square(double num) {
return num * num;
}
public static void main(String[] args) throws InterruptedException {
String address = "http://192.168.2.66:8080/demo";
Endpoint.publish(address, new DemoHttpServer());
System.out.println("Service running at " + address);
System.out.println("Type Ctrl+c to exit");
Thread.sleep(Long.MAX_VALUE);
}
}
但是,当我在浏览器中点击URL时,我得到ERR_EMPTY_RESPONSE
。 谁能告诉我我想念什么? 我不想为此使用任何外部库。
您没有写出导致ERR_EMPTY_RESPONSE
错误的确切URL,但是您说wsdl
在工作,因此WS应该工作。
您有一个SOAP WebService,因此可以使用soapui或postman (另请参见此处 )来调用您的服务(位于http://192.168.2.66:8080/demo/DemoHttpServerService
)。
您还可以从WSDL生成JAX-WS Java客户端。 例如,在Eclipse中,有一个用于该工具的工具。 对于您的WS真正的Java使用,您应该使用此名称或类似名称。
最后,您可以编写一个简单的程序来调用您的服务,仅用于测试(从此处改编):
package another;
import javax.jws.WebService;
@WebService
public interface IWebServiceTest {
double square(double num);
}
package another;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
public class Main {
public static void main(String[] args) throws Exception {
String url = "http://192.168.2.66:8080/demo/DemoHttpServerService?wsdl";
String namespace = "http://another/";
QName serviceQN = new QName(namespace, "DemoHttpServerService");
Service service = Service.create(new URL(url), serviceQN);
String portName = "DemoHttpServerPort";
QName portQN = new QName(namespace, portName);
IWebServiceTest sample = service.getPort(portQN, IWebServiceTest.class);
double result = sample.square(3);
System.out.println(result);
}
}
在项目中尝试使用Java包名称时,请注意该名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.