使用 Jersey 和仅 Java 创建一个简单的 html 服务器
Creating a simple html server with Jersey and Java only
不知道为什么我在寻找一种使用 Jersey 制作简单网络服务器的方法时遇到了这么多麻烦。
public class AnotherJerseyHttpServer {
public static void main(String[] args) throws IOException {
System.out.println("Starting Crunchify's Embedded Jersey HTTPServer...\n");
HttpServer webServer = createHttpServer();
webServer.start();
System.out.println(String.format("\nJersey Application Server started with WADL available at " + "%sapplication.wadl\n", getURI()));
System.out.println("Started Crunchify's Embedded Jersey HTTPServer Successfully !!!");
}
public static HttpServer createHttpServer() throws IOException {
ResourceConfig rc = new PackagesResourceConfig("com.daford");
// This tutorial required and then enable below line: http://crunfy.me/1DZIui5
//rc.getContainerResponseFilters().add(CrunchifyCORSFilter.class);
return HttpServerFactory.create(getURI(), rc);
}
private static URI getURI() {
return UriBuilder.fromUri("http://" + sHostname() + "/").port(4444).build();
}
private static String sHostname() {
String hostName = "localhost";
try {
hostName = InetAddress.getLocalHost().getCanonicalHostName();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return hostName;
}
}
和
@Path("api")
public class RestAPI {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String get() {
return "this works";
}
}
而且我可以做到 http://localhost:4444/api 并得到 "this works"。现在如何允许基于传入的 URL 的 html 文件?我得到了各种 MediaType/mimi 信息,但我在网上找不到任何东西告诉我如何根据传入的 URL.
return 文件
假设你需要return的文件打包在你的WAR文件中,你可以尝试以下解决方案:
使用Google Guava
@Path("/{fileName: .+}")
@Produces(MediaType.TEXT_HTML)
public class HtmlResource {
@GET
public Response getPage(@PathParam("fileName") String fileName) throws IOException {
URL url = Resources.getResource(fileName);
return Response.ok(Resources.toString(url, Charsets.UTF_8)).build();
}
}
使用普通 Java 代码
@Path("/{fileName: .+}")
@Produces(MediaType.TEXT_HTML)
public class HtmlResource {
@GET
public Response getPage(@PathParam("fileName") String fileName) throws IOException {
InputStream stream = HtmlResource.class.getClassLoader()
.getResourceAsStream(fileName);
String responseContent = read(stream);
return Response.ok(responseContent).build();
}
private String read(InputStream stream) throws IOException {
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(
stream, StandardCharsets.UTF_8))) {
return buffer.lines().collect(Collectors.joining("\n"));
}
}
}
例如,fileName
可以是 assets/index.html
。
不知道为什么我在寻找一种使用 Jersey 制作简单网络服务器的方法时遇到了这么多麻烦。
public class AnotherJerseyHttpServer {
public static void main(String[] args) throws IOException {
System.out.println("Starting Crunchify's Embedded Jersey HTTPServer...\n");
HttpServer webServer = createHttpServer();
webServer.start();
System.out.println(String.format("\nJersey Application Server started with WADL available at " + "%sapplication.wadl\n", getURI()));
System.out.println("Started Crunchify's Embedded Jersey HTTPServer Successfully !!!");
}
public static HttpServer createHttpServer() throws IOException {
ResourceConfig rc = new PackagesResourceConfig("com.daford");
// This tutorial required and then enable below line: http://crunfy.me/1DZIui5
//rc.getContainerResponseFilters().add(CrunchifyCORSFilter.class);
return HttpServerFactory.create(getURI(), rc);
}
private static URI getURI() {
return UriBuilder.fromUri("http://" + sHostname() + "/").port(4444).build();
}
private static String sHostname() {
String hostName = "localhost";
try {
hostName = InetAddress.getLocalHost().getCanonicalHostName();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return hostName;
}
}
和
@Path("api")
public class RestAPI {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String get() {
return "this works";
}
}
而且我可以做到 http://localhost:4444/api 并得到 "this works"。现在如何允许基于传入的 URL 的 html 文件?我得到了各种 MediaType/mimi 信息,但我在网上找不到任何东西告诉我如何根据传入的 URL.
return 文件假设你需要return的文件打包在你的WAR文件中,你可以尝试以下解决方案:
使用Google Guava
@Path("/{fileName: .+}")
@Produces(MediaType.TEXT_HTML)
public class HtmlResource {
@GET
public Response getPage(@PathParam("fileName") String fileName) throws IOException {
URL url = Resources.getResource(fileName);
return Response.ok(Resources.toString(url, Charsets.UTF_8)).build();
}
}
使用普通 Java 代码
@Path("/{fileName: .+}")
@Produces(MediaType.TEXT_HTML)
public class HtmlResource {
@GET
public Response getPage(@PathParam("fileName") String fileName) throws IOException {
InputStream stream = HtmlResource.class.getClassLoader()
.getResourceAsStream(fileName);
String responseContent = read(stream);
return Response.ok(responseContent).build();
}
private String read(InputStream stream) throws IOException {
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(
stream, StandardCharsets.UTF_8))) {
return buffer.lines().collect(Collectors.joining("\n"));
}
}
}
例如,fileName
可以是 assets/index.html
。