Grizzly和ServletContainerContext [英] Grizzly and ServletContainerContext

查看:143
本文介绍了Grizzly和ServletContainerContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在我编写的Servlet中获取一些注入的上下文(例如Session或HttpServletRequest),在Grizzly上运行,但我做的任何事情似乎都没有用。整个过程似乎过早停顿,出现以下错误:

I'm trying to get hold of some injected context (for example Session or HttpServletRequest) in a Servlet I've written, running on Grizzly, but nothing I do seems to work. The whole process seems to stall rather prematurely with the following error:

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

服务器很简单,它由两个文件组成,静态入口点(Main.java):

The server is dead simple, it consists of two files, the static entry point (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):

and the 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-在上面的源文件中的行,它是我所有问题的来源和解决方案。我需要它,根据我读过的关于泽西岛和Servlets的一切,它应该有用,但唉它没有。我也尝试使用GrizzlyWebContainerFactory而不是GrizzlyServerFactory,但无济于事。

Specifically, it's the @Context-line in the above source file that is the source and solution to all my problems. I need it, and according to everything I've read about Jersey and Servlets it should work, but alas it does not. I've also tried using GrizzlyWebContainerFactory instead of the GrizzlyServerFactory, but to no avail.

作为参考,项目使用以下依赖项进行编译:

For reference, the project is compiled with the following dependencies:


  • 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-grizzly2-servlet:jar:1.17

  • asm:asm:jar:3.3.1

  • 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类适用于我:

This Main class works fine for me:

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的上下文路径。

Try http://localhost:8080/lol/cat in your browser. You can change JERSEY_SERVLET_CONTEXT_PATH to update Servlet's context-path.

这篇关于Grizzly和ServletContainerContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆