JWebserver - 启动 Java 简单 Web 服务器
介绍 jwebserver
jwebserver - 一个提供最小 HTTP 服务器的工具,用于服务单个目录层次结构,旨在用于原型设计、测试和调试。
概要
jwebserver [options]
options
请参阅以下 jwebserver
的选项。
以下选项可用
--bind-address addr
或 —b addr
指定要绑定的地址。默认地址为 127.0.0.1
或 ::1
(环回)。如果您需要所有接口,可以使用 -b 0.0.0.0
或 -b ::
。
--directory dir
或 —d dir
指定要服务的目录。默认值为当前目录。
--help
或 —h
打印完整帮助消息。
--output level
或 —o level
指定输出格式,可以是 none | info | verbose
之一。默认值为 info
。
--port port
或 -p port
指定要监听的端口。默认端口值为 8000。
--version
打印工具的简短版本字符串。
描述
jwebserver
是一个 JDK 工具,提供一个最小的 HTTP 服务器,您可以使用它进行原型设计、测试和调试。该工具仅服务静态文件,并通过 HTTP/1.1 查看单个目录层次结构;不支持动态内容和其他 HTTP 版本。模块 jdk.httpserver
包含 jwebserver
,并且可以使用 java -m jdk.httpserver
启动,因为它基于 com.sun.net.httpserver
包中的 Web 服务器实现。该 SimpleFileServer 类提供了一种以编程方式检索服务器及其组件以供重用和扩展的方法。查看 使用简单 Web 服务器 API 部分。
服务请求的方法仅是幂等的 HEAD
和 GET
。如果您尝试任何其他请求,您将收到 501 - 未实现
或 405 - 不允许
响应。该工具将 GET 请求映射到正在服务的目录,如下所示
- 如果请求的资源是文件,则会服务其内容。
- 如果请求的资源是包含索引文件的目录,则会服务该索引文件的内容。
- 否则,响应将包含目录中所有文件和子目录的名称。符号链接和隐藏文件不会被列出或服务。
jwebserver
使用内置表自动配置 MIME 类型。例如,.html
文件将作为 text/html
服务,而 .java
文件将作为 text/plain
服务。
命令行示例
jwebserver
可以帮助您
Web 开发测试,当您需要本地测试服务器来模拟客户端-服务器设置时。
jwebserver Binding to loopback by default. For all interfaces use "-b 0.0.0.0" or "-b ::". Serving /cwd and subdirectories on 127.0.0.1 port 8000 URL: http://127.0.0.1:8000/
Web 服务或应用程序测试,您在其中使用静态文件作为目录结构中的 API 存根来镜像 RESTful URL 并包含虚拟数据。
jwebserver -p 9000
Binding to loopback by default. For all interfaces use "-b 0.0.0.0" or "-b ::".
Serving /youtube-jdk21 and subdirectories on 127.0.0.1 port 9000
URL http://127.0.0.1:9000/
127.0.0.1 - - [09/Jul/2023:13:02:05 +0200] "GET /api/activity.json HTTP/1.1" 200 -
127.0.0.1 - - [09/Jul/2023:13:02:06 +0200] "GET /api/activity.json HTTP/1.1" 200 -
- 非正式浏览和跨系统共享文件,例如,从本地机器搜索远程服务器上的目录。
jwebserver -b 0.0.0.0
Serving /work and subdirectories on 0.0.0.0 (all interfaces) port 8000
URL http://192.168.178.41:8000/
虽然命令行工具很有用,但您也可以通过其 API 使用简单 Web 服务器(即服务器、处理程序和过滤器)与现有代码一起使用。
使用简单 Web 服务器 API
除了 jwebserver
命令行工具之外,简单 Web 服务器还提供了一个 API,以便您可以以编程方式创建和自定义服务器及其组件。之前,您观察到命令 jwebserver
如何服务当前工作目录的文件。但是,您有时需要该目录结构来模拟预期的响应模式,并且在物理上不保留模拟。您可以通过使用 Google 的 Java 内存文件系统 Jimfs
来创建内存中资源并使用简单 Web 服务器为它们提供服务来实现这种行为
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.UUID;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.sun.net.httpserver.SimpleFileServer;
import static com.sun.net.httpserver.SimpleFileServer.OutputLevel;
public class InMemoryFileServer {
private static final InetSocketAddress LOOPBACK_ADDR =
new InetSocketAddress(InetAddress.getLoopbackAddress(), 8080);
public static final String JSON_FILE_NAME = "thing.json";
public static final String DIR_PATH = "some/other";
public static void main( String[] args ) throws Exception {
Path root = createDirectoryHierarchy();
var server = SimpleFileServer.createFileServer(LOOPBACK_ADDR, root, OutputLevel.VERBOSE);
server.start();
System.out.printf("http://%s:%d%n", server.getAddress().getHostString(), server.getAddress().getPort());
}
private static Path createDirectoryHierarchy() throws IOException {
String json = """
{
"activity": "Go for a run",
"type": "recreational",
"participants": 1,
"distance": "%d km",
"key": "%s",
}
""";
FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
Path root = fs.getPath("/");
Path dir = Files.createDirectories(root.resolve(DIR_PATH));
Path filePath = fs.getPath(JSON_FILE_NAME);
Path innerMockFile = Files.createFile(dir.resolve(filePath));
Files.write(innerMockFile,json.formatted(12, UUID.randomUUID().toString()).getBytes(), StandardOpenOption.WRITE);
Path mockFile = Files.createFile(dir.getParent().resolve(filePath));
Files.write(mockFile,json.formatted(10, UUID.randomUUID().toString()).getBytes(), StandardOpenOption.WRITE);
return root;
}
}
通过以下方式在命令行中运行前面的代码片段
$ java InMemoryFileServer.java
> http://127.0.0.1:8080
上面的示例服务存储在服务器内存中的 JSON 响应,从而节省磁盘空间,同时提供 API 存根 所需的内容。您可以在 Christian Stein 的文章 中找到另一种在内存中服务资产的方法,他在其中实现 com.sun.net.httpserver.HttpHandler
来服务内存中资产。有关使用简单 Web 服务器 API 的更多编程示例,请参阅 Julia Boes 在 https://inside.java-lang.cn 上的文章。
有用链接
- 内存中 HttpServer 处理程序:https://inside.java-lang.cn/2023/11/06/in-memory-http-server-handler/
- 使用简单 Web 服务器:https://inside.java-lang.cn/2021/12/06/working-with-the-simple-web-server
更多学习
上次更新: 2024 年 1 月 4 日