【问题标题】:Simple Server for Hosting XML in Java用于在 Java 中托管 XML 的简单服务器
【发布时间】:2012-05-16 00:23:45
【问题描述】:

我有一些简单的动态 XML,我想使用 Java 在端点上通过 http 出售(例如:http://localhost:8888/getUpdatedInfo)。

我真的很想避免使用框架或第三方库。我知道对于 WSDL 端点,使用普通 JDK 托管服务器非常简单(在 http://docs.oracle.com/javaee/6/api/index.html?javax/xml/ws/Endpoint.html 中使用 endpoint.publish())

对于任意 HTML/XML 是否有类似的东西?

【问题讨论】:

  • 为什么还要使用 Java 来做类似的事情。似乎有点矫枉过正。一个简单的 Apache 服务器可以解决问题,并避免让您的 IT 部门与 JVM 决战的所有依赖项指甲。 :)
  • 你可以为java使用restful web services,但是apache会更简单。
  • 我很久以前认识的一个人组装了一个微型 HTTP 服务器,结果证明它非常适合提供静态内容:acme.com/software/thttpd

标签: xml jax-ws java


【解决方案1】:

documentation for com.sun.net.httpserver中所见:

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

public class SimpleHttpServer {
    public static void main(String[] args) throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress(8888), 0);
        server.createContext("/foo", new HttpHandler() {
            public void handle(HttpExchange t) throws IOException {
                t.sendResponseHeaders(200, 0);
                OutputStream out = t.getResponseBody();
                out.write("hello world".getBytes());
                out.close();
            }
        });
        server.start();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 2016-12-15
    • 2014-09-17
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    相关资源
    最近更新 更多