【问题标题】:Grizzly Server with static content and REST resource具有静态内容和 REST 资源的 Grizzly 服务器
【发布时间】:2014-01-22 08:49:37
【问题描述】:

我有一个 Jersey REST 2.5.1 服务,它通过 Grizzly 服务器提供服务。到目前为止一切正常。我想添加一些静态内容,这些内容也通过 Grizzly 提供并从我的 JAR 文件中提供。因此我使用CLStaticHttpHandler。当我显式访问静态资源,例如我的index.html(例如http://localhost:8080/index.html)时,一切正常。但是,当我尝试访问根 http://localhost:8080 时,我得到了 404。代码如下所示:

ObjectMapper mapper = new ObjectMapper();
// some configuration stuff here

JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
provider.setMapper(mapper);

ResourceConfig resourceConfig = new ResourceConfig()
            .packages("my.restapi.package")
            .register(provider);

HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), resourceConfig);
HttpHandler httpHandler = new CLStaticHttpHandler(HttpServer.class.getClassLoader(), "/static/");
httpServer.getServerConfiguration().addHttpHandler(httpHandler, "/");

据我从调试中得知,org.glassfish.grizzly.http.server.CLStaticHttpHandler.handle(String, Request, Response) 永远不会被调用。任何提示,我怎样才能使index.html 作为默认页面访问?

【问题讨论】:

    标签: java grizzly jersey-2.0


    【解决方案1】:

    在浪费了几个小时之后,我现在觉得有点愚蠢,但简单的解决方案是,在BASE_URI 中指定路径(http://localhost:8080/api/ 而不是http://localhost:8080/)。现在,当访问/ 时,我得到index.html 并且REST 方法在/api 下。

    【讨论】:

    • 是的,我就是这么用的。
    • @qqilihq 你把静态文件夹放在哪里?
    • 在项目的类路径中(我当时使用的是一个maven项目,所以我把/static放在src/main/resources中)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    相关资源
    最近更新 更多