2013-08-21 8 views
5

के भीतर से स्थिर सामग्री की सेवा करता है मैं ग्रिज़ली के com.sun.grizzly.http.embed.GrizzlyWebServer का उपयोग कर जर्सी-स्प्रिंग आधारित आरईएसटी एपीआई को तैनात करने की कोशिश कर रहा हूं। मैं भी इसका उपयोग कर स्थिर सामग्री की सेवा करना चाहता हूं।ग्रीज़लीवेबसेवर + स्प्रिंग + जर्सी + जार

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

"जर्सी एडाप्टर" ठीक काम करता है, लेकिन मैं पेश किए जाएं, "webapp" फ़ोल्डर में स्थिर सामग्री वर्तमान प्राप्त करने में सक्षम (404 त्रुटि) नहीं कर रहा हूँ: यहाँ मैं क्या है।

मेरे परियोजना फ़ोल्डर संरचना इस प्रकार है: मैं लाइन new GrizzlyWebServer(host, port, "webapp", false); में "webapp" के लिए पथ प्रदान करने में एक गलती कर

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

Am ??

या, स्थिर सामग्री की सेवा करने का कोई और तरीका है ??

उत्तर

7

जर्सी-स्प्रिंग संसाधनों + फ़ोल्डर से स्थिर सामग्री और ग्रिज़ली 2 के शीर्ष पर काम कर रहे एक जार फ़ाइल से कैसे एक नमूना है।

// 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

धन्यवाद एलेक्सी:

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

सर्वर कोड की तरह दिखता है। मेरे पास जार से स्थैतिक संसाधनों के अनुरोध पर प्रमाणीकरण के बारे में एक सवाल है, इसलिए CLStaticHttpHandler बनाम StaticHttpHandler के माध्यम से। यह अप्रमाणित है लेकिन ऐसा लगता है कि प्रमाणीकरण पूर्व पर लागू नहीं होता है लेकिन बाद में लागू होता है? 'WebappContext' में यह पंक्ति यह समझा सकती है:' if (! (H exampleof StaticHttpHandler)) '(grizzly-http-servlet-2.3.17.jar)। विचार? धन्यवाद। – user598656

+0

अच्छी तरह से, जो आपको मिला वह ऐसा लगता है कि कुछ समस्याएं हो सकती हैं। क्या आप pls कर सकते हैं एक github पर टेस्टकेस साझा करें - मेरे लिए डबल चेक करना आसान होगा। – alexey

संबंधित मुद्दे