[英]GrizzlyWebServer + Spring + Jersey + serve static content from within JAR
I'm trying to deploy Jersey-Spring based REST API using Grizzly's com.sun.grizzly.http.embed.GrizzlyWebServer
. 我正在尝试使用Grizzly的
com.sun.grizzly.http.embed.GrizzlyWebServer
部署基于Jersey-Spring的REST API。 I also want to serve static content using the same. 我也想使用相同的服务提供静态内容。 Here is what I have:
这是我有的:
String host = "localhost";
int port = 8081;
// For jersey + Spring
ServletAdapter jAdapter = new ServletAdapter("jersey");
jAdapter.setContextPath("/api");
jAdapter.setServletInstance(new SpringServlet());
jAdapter.addContextParameter("contextConfigLocation", "classpath:spring-context.xml");
jAdapter.addServletListener("org.springframework.web.context.ContextLoaderListener");
jAdapter.addServletListener("org.springframework.web.context.request.RequestContextListener");
// create GrizzlyWebServer
GrizzlyWebServer grizzlyServer = new GrizzlyWebServer(host, port, "webapp", false);
// add jersey adapter
grizzlyServer.addGrizzlyAdapter(jAdapter, new String[]{"/api"});
// start server
grizzlyServer.start();
System.out.println("Start running server(host: " + host + ",port: " + Integer.toString(port));
System.out.println("Press any key to stop the server.");
// hang on
System.in.read();
// stop
grizzlyServer.stop();
The "Jersey Adapter" works fine but I am not able to get the static content present in "webapp" folder to be served (404 Error). “Jersey Adapter”工作正常但我无法获取“webapp”文件夹中存在的静态内容(404错误)。
My project folder structure is as follows: 我的项目文件夹结构如下:
GrizzlyTest
-- src
| |
| -- main
| |
| -- java
| -- resources
| |
| -- webapp
| | |
| | -- index.html
| -- spring-context.xml
|
-- pom.xml
Am I making a mistake in providing the path for "webapp" in the line new GrizzlyWebServer(host, port, "webapp", false);
我在为
new GrizzlyWebServer(host, port, "webapp", false);
提供“webapp”的路径时犯了一个错误new GrizzlyWebServer(host, port, "webapp", false);
?? ??
Or, is there any other way to serve static content ?? 或者,有没有其他方式来提供静态内容?
Here is a sample on how to serve Jersey-Spring resources + static content from a folder and from a jar file working on top of Grizzly2. 下面是一个示例,介绍如何从文件夹和Grizzly2上运行的jar文件中提供Jersey-Spring资源+静态内容。
https://github.com/oleksiys/samples/tree/master/jersey1-grizzly2-spring https://github.com/oleksiys/samples/tree/master/jersey1-grizzly2-spring
The server code looks like: 服务器代码如下所示:
// Initialize Grizzly HttpServer
HttpServer server = new HttpServer();
NetworkListener listener = new NetworkListener("grizzly2", "localhost", 3388);
server.addListener(listener);
// Initialize and add Spring-aware Jersey resource
WebappContext ctx = new WebappContext("ctx", "/api");
final ServletRegistration reg = ctx.addServlet("spring", new SpringServlet());
reg.addMapping("/*");
ctx.addContextInitParameter("contextConfigLocation", "classpath:spring-context.xml");
ctx.addListener("org.springframework.web.context.ContextLoaderListener");
ctx.addListener("org.springframework.web.context.request.RequestContextListener");
ctx.deploy(server);
// Add the StaticHttpHandler to serve static resources from the static1 folder
server.getServerConfiguration().addHttpHandler(
new StaticHttpHandler("src/main/resources/webapp/static1/"), "/static");
// Add the CLStaticHttpHandler to serve static resources located at
// the static2 folder from the jar file jersey1-grizzly2-spring-1.0-SNAPSHOT.jar
server.getServerConfiguration().addHttpHandler(
new CLStaticHttpHandler(new URLClassLoader(new URL[] {
new File("target/jersey1-grizzly2-spring-1.0-SNAPSHOT.jar").toURI().toURL()}), "webapp/static2/"),
"/jarstatic");
try {
server.start();
System.out.println("In order to test the server please try the following urls:");
System.out.println("http://localhost:3388/api to see the default TestResource.getIt() resource");
System.out.println("http://localhost:3388/api/test to see the TestResource.test() resource");
System.out.println("http://localhost:3388/api/test2 to see the TestResource.test2() resource");
System.out.println("http://localhost:3388/static/ to see the index.html from the webapp/static1 folder");
System.out.println("http://localhost:3388/jarstatic/ to see the index.html from the webapp/static2 folder served from the jar file");
System.out.println();
System.out.println("Press enter to stop the server...");
System.in.read();
} finally {
server.shutdownNow();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.