java Grizzly 和 ServletContainerContext
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15769415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Grizzly and ServletContainerContext
提问by Mic
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:
我试图在我编写的 Servlet 中获取一些注入的上下文(例如 Session 或 HttpServletRequest),在 Grizzly 上运行,但我所做的一切似乎都不起作用。整个过程似乎因以下错误而过早地停止:
SEVERE: Missing dependency for field: javax.servlet.http.HttpServletRequest com.test.server.LolCat.hsr
The server is dead simple, it consists of two files, the static entry point (Main.java):
服务器非常简单,它由两个文件组成,静态入口点(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();
}
}
and the serlvet (LolCat.java):
和 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";
}
}
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.
具体来说,上面源文件中的@Context-line 是我所有问题的来源和解决方案。我需要它,根据我读过的关于 Jersey 和 Servlets 的所有内容,它应该可以工作,但可惜它没有。我也尝试过使用 GrizzlyWebContainerFactory 而不是 GrizzlyServerFactory,但无济于事。
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: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
- 汇编:汇编:罐:3.3.1
采纳答案by alexey
This Main class works fine for me:
这个 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();
}
}
Try http://localhost:8080/lol/cat
in your browser.
You can change JERSEY_SERVLET_CONTEXT_PATH to update Servlet's context-path.
http://localhost:8080/lol/cat
在浏览器中尝试。您可以更改 JERSEY_SERVLET_CONTEXT_PATH 以更新 Servlet 的上下文路径。
回答by Alfishe
As per developers explanations - Grizzly is not fully compliant to JAX-RS 2.0 so there will be no official contexts injections/wrapping. See Jersey Bug-1960Applicable for Jersey + Grizzly version 2.7+
根据开发人员的解释 - Grizzly 不完全符合 JAX-RS 2.0,因此不会有官方上下文注入/包装。请参阅Jersey Bug-1960适用于 Jersey + Grizzly 版本 2.7+
Luckily there is a way to inject Grizzly request/response objects. Kind of tricky but works Code sample provided in one of Jersey's unit tests. See Jersey container test
幸运的是,有一种方法可以注入 Grizzly 请求/响应对象。有点棘手但有效泽西岛的单元测试之一中提供的代码示例。参见Jersey 容器测试
So code fragment will be:
所以代码片段将是:
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
}
}
}
Works fine both for filters and service methods
适用于过滤器和服务方法
回答by rodnei couto
You can also manually register a ResourceContext
也可以手动注册一个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);
Where config is your resource context.
其中 config 是您的资源上下文。
回答by Sumit Kumar Saha
Try something like this :-
尝试这样的事情:-
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();
}
}