Java jax-rs server



当我在本地测试我创建的SOAP Web服务时,我通常会这样做:

MyServiceImpl service = new MyServiceImpl();
Endpoint endpoint=Endpoint.create(service);
endpoint.publish("http://localhost:8084/service") ;

现在我也想将其扩展到 REST 服务。我可以用 JBoss 做到这一点,但我也想在之前进行本地测试。有什么方法可以在Java中启动Jax-RS Rest HTTP服务(并且没有其他第三方库,如jersey等)。

如果不添加依赖项,就无法做到这一点。简单的方法是简单框架和泽西简单服务器。将它们添加到 maven 的配置文件中,您仍然可以在没有它们的情况下部署到 JBoss。

您可以通过手动打开http连接,有很多方法可以做到这一点

下面是更多示例之一。

import java.net.*;
import java.io.*;
public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://www.sum.com/");
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

最新更新