2013-08-21 70 views
5

我試圖使用Grizzly的com.sun.grizzly.http.embed.GrizzlyWebServer部署基於Jersey-Spring的REST API。我也想使用相同的靜態內容。以下是我有:GrizzlyWebServer + Spring + Jersey +從JAR內提供靜態內容

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(); 

的「澤西適​​配器」工作正常,但我不能以獲取靜態內容出現在「Web應用程序」文件夾送達(404錯誤)。

我的項目文件夾結構如下:

GrizzlyTest 
    -- src 
    | | 
    | -- main 
    |  | 
    |  -- java 
    |  -- resources 
    |  | 
    |  -- webapp 
    |  | | 
    |  | -- index.html 
    |  -- spring-context.xml 
    | 
    -- pom.xml 

上午我在行new GrizzlyWebServer(host, port, "webapp", false);提供「Web應用程序」的路徑犯了一個錯誤?

或者,有沒有其他的方式來提供靜態內容?

回答

7

以下是關於如何從文件夾和工作在Grizzly2之上的jar文件爲Jersey-Spring資源+靜態內容提供服務的示例。

https://github.com/oleksiys/samples/tree/master/jersey1-grizzly2-spring

服務器的代碼如下所示:

// 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(); 
} 
+0

感謝阿列克謝。我有一個關於來自jar的靜態資源請求認證的問題,所以通過CLStaticHttpHandler與StaticHttpHandler。這是未經證實的,但它看起來認證不適用於前者,但適用於後者? 'WebappContext'中的這一行可以解釋爲:'if(!(h instanceof StaticHttpHandler))'(grizzly-http-servlet-2.3.17.jar)。思考?謝謝。 – user598656

+0

好吧,從你發現它看起來可能會有一些問題。你可以請。在github上共享測試用例 - 對我來說更容易進行雙重檢查。 – alexey