Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GrizzlyWebServer+;弹簧&x2B;泽西岛+;从JAR中提供静态内容_Java_Rest_Jersey_Grizzly_Static Content - Fatal编程技术网

Java GrizzlyWebServer+;弹簧&x2B;泽西岛+;从JAR中提供静态内容

Java GrizzlyWebServer+;弹簧&x2B;泽西岛+;从JAR中提供静态内容,java,rest,jersey,grizzly,static-content,Java,Rest,Jersey,Grizzly,Static Content,我正在尝试使用Grizzly的com.sun.Grizzly.http.embed.GrizzlyWebServer部署基于Jersey-Spring的restapi。我还希望使用相同的方法提供静态内容。以下是我所拥有的: String host = "localhost"; int port = 8081; // For jersey + Spring ServletAdapter jAdapter = new ServletAdapter("jersey"); jAdapter.setCo

我正在尝试使用Grizzly的
com.sun.Grizzly.http.embed.GrizzlyWebServer
部署基于Jersey-Spring的restapi。我还希望使用相同的方法提供静态内容。以下是我所拥有的:

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();
“Jersey Adapter”工作正常,但我无法获取“webapp”文件夹中的静态内容(404错误)

我的项目文件夹结构如下:

GrizzlyTest
  -- src
  |  |
  |  -- main
  |     |
  |     -- java
  |     -- resources
  |        |
  |        -- webapp
  |        |  |
  |        |  -- index.html
  |        -- spring-context.xml
  |
  -- pom.xml
在新GrizzlyWebServer的
行中为“webapp”提供路径是否有误(主机、端口,“webapp”,false)


或者,是否有其他方式提供静态内容???

以下是一个示例,介绍如何从文件夹和Grizzly2上的jar文件中提供Jersey 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();
}

谢谢阿列克西。我有一个关于从JAR请求静态资源的身份验证的问题,所以是通过CLStaticHttpHandler还是通过StaticHttpHandler。这是未经证实的,但似乎认证不适用于前者,而是适用于后者?
WebappContext
中的这一行可以解释这一点:
如果(!(StaticHttpHandler的h instanceof))
(grizzly-http-servlet-2.3.17.jar)。思想?谢谢。嗯,从你的发现来看,可能有一些问题。可以吗。在github上共享测试用例-这将使我更容易进行双重检查。