簡體   English   中英

Grizzly 和 ServletContainerContext

[英]Grizzly and ServletContainerContext

我試圖在我編寫的在 Grizzly 上運行的 Servlet 中獲取一些注入的上下文(例如 Session 或 HttpServletRequest),但我所做的一切似乎都不起作用。 整個過程似乎因以下錯誤而過早地停止:

SEVERE: Missing dependency for field: javax.servlet.http.HttpServletRequest com.test.server.LolCat.hsr

服務器非常簡單,它由兩個文件組成,靜態入口點(Main.java):

package com.test.server;

import java.io.IOException;
import java.net.URI;
import javax.ws.rs.core.UriBuilder;

import org.glassfish.grizzly.http.server.HttpServer;
import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.ClassNamesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;

public class Main {

    private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost/").port(8080).build();
    }

    public static final URI BASE_URI = getBaseURI();

    public static void main(String[] args) throws IOException {
        ResourceConfig rc = new ClassNamesResourceConfig(LolCat.class);
        HttpServer httpServer = GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
        System.in.read();
        httpServer.stop();
    }
}

和 serlvet (LolCat.java):

package com.test.server;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

@Path(value = "/lol")
public class LolCat {

    @Context HttpServletRequest hsr;

    @GET
    @Path(value="/cat")
    public String list() {
        return "meow";
    }

}

具體來說,上面源文件中的@Context-line 是我所有問題的來源和解決方案。 我需要它,根據我讀過的關於 Jersey 和 Servlets 的所有內容,它應該可以工作,但遺憾的是它沒有。 我也嘗試過使用 GrizzlyWebContainerFactory 而不是 GrizzlyServerFactory,但無濟於事。

作為參考,該項目使用以下依賴項進行編譯:

  • org.glassfish.grizzly:grizzly-framework:jar:2.2.21
  • org.glassfish.grizzly:grizzly-http:jar:2.2.21
  • org.glassfish.grizzly:grizzly-http-servlet:jar:2.2.21
  • org.glassfish.grizzly:grizzly-http-server:jar:2.2.21
  • com.sun.jersey:jersey-server:jar:1.17
  • com.sun.jersey:jersey-servlet:jar:1.17
  • com.sun.jersey:jersey-core:jar:1.17
  • javax.servlet:javax.servlet-api:jar:2.5.0
  • com.sun.jersey:jersey-grizzly2:jar:1.17
  • com.sun.jersey:jersey-grizzly2-servlet:jar:1.17
  • asm:asm:jar:3.3.1

這個 Main 類對我來說很好用:

package com.test.server;

import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import java.io.IOException;
import java.net.URI;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.core.ClassNamesResourceConfig;
import com.sun.jersey.spi.container.servlet.ServletContainer;
import org.glassfish.grizzly.http.server.HttpHandler;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.Request;
import org.glassfish.grizzly.http.server.Response;
import org.glassfish.grizzly.servlet.ServletRegistration;
import org.glassfish.grizzly.servlet.WebappContext;

public class Main {
    private static final String JERSEY_SERVLET_CONTEXT_PATH = "";

    private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost").port(8080).path("/").build();
    }

    public static final URI BASE_URI = getBaseURI();

    public static void main(String[] args) throws IOException {
        // Create HttpServer and register dummy "not found" HttpHandler
        HttpServer httpServer = GrizzlyServerFactory.createHttpServer(BASE_URI, new HttpHandler() {

            @Override
            public void service(Request rqst, Response rspns) throws Exception {
                rspns.setStatus(404, "Not found");
                rspns.getWriter().write("404: not found");
            }
        });

        // Initialize and register Jersey Servlet
        WebappContext context = new WebappContext("WebappContext", JERSEY_SERVLET_CONTEXT_PATH);
        ServletRegistration registration = context.addServlet("ServletContainer", ServletContainer.class);
        registration.setInitParameter(ServletContainer.RESOURCE_CONFIG_CLASS, 
                ClassNamesResourceConfig.class.getName());
        registration.setInitParameter(ClassNamesResourceConfig.PROPERTY_CLASSNAMES, LolCat.class.getName());
        registration.addMapping("/*");
        context.deploy(httpServer);

        System.in.read();
        httpServer.stop();
    }
}

在瀏覽器中嘗試http://localhost:8080/lol/cat 您可以更改 JERSEY_SERVLET_CONTEXT_PATH 以更新 Servlet 的上下文路徑。

根據開發人員的解釋 - Grizzly 不完全符合 JAX-RS 2.0,因此不會有官方上下文注入/包裝。 請參閱Jersey Bug-1960適用於 Jersey + Grizzly 版本 2.7+

幸運的是,有一種方法可以注入 Grizzly 請求/響應對象。 有點棘手但有效澤西島的單元測試之一中提供的代碼示例。 參見Jersey 容器測試

所以代碼片段將是:

import javax.inject.Inject;
import javax.inject.Provider;

public someclass {
    @Inject
    private Provider<Request> grizzlyRequestProvider;

    public void method() {
         if (grizzlyRequestProvider != null) {
            Request httpRequest = grizzlyRequestProvider.get();

            // Extract what you need
         }
    }
}

適用於過濾器和服務方法

你也可以手動注冊一個ResourceContext

  HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(getBaseURI());

  WebappContext context = new WebappContext("WebappContext", "/api");
  ServletRegistration registration = context.addServlet("ServletContainer",
    new ServletContainer(config));
  registration.addMapping("/*");
  context.deploy(httpServer);

其中 config 是您的資源上下文。

嘗試這樣的事情:-

public class Main {

    private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost/").port(8080).build();
    }

    public static void main(String[] args) throws IOException {
        ResourceConfig rc = new ResourceConfig().packages("com.example");//path to you class files
        HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(getBaseURI(), rc);
        System.in.read();
        httpServer.stop();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM