缺少HttpServletRequest与Jersey的依赖项
我有一个独立的Jersey服务器在JunitTest开始时运行。我正在测试我的HttpClient和我的定制HttpClient是否正常工作。请注意,我一直能够使用嵌入glassfish中的这个JAXRResourceController 这是JaxRsController(轻型版本) 基本上,它说在@Context注入时,对HttpServletRequest没有依赖性。 但是,如果在请求和响应时删除@Context注释,但将其保留在缺少HttpServletRequest与Jersey的依赖项,jersey,Jersey,我有一个独立的Jersey服务器在JunitTest开始时运行。我正在测试我的HttpClient和我的定制HttpClient是否正常工作。请注意,我一直能够使用嵌入glassfish中的这个JAXRResourceController 这是JaxRsController(轻型版本) 基本上,它说在@Context注入时,对HttpServletRequest没有依赖性。 但是,如果在请求和响应时删除@Context注释,但将其保留在UriInfo-Context中,则可以读取Uri 我改变了
UriInfo-Context
中,则可以读取Uri
我改变了几次Maven pom wich现在是为了迫使LIB加入:
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.14</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
泽西岛
泽西服务器
1.14
javax.servlet
servlet api
2.5
javax.ws.rs
jsr311 api
1.1.1
javax.servlet.jsp
jsp api
2.1
有什么想法吗?servlet依赖项被分离到另一个模块,请尝试添加
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.14</version>
</dependency>
泽西岛
泽西servlet
1.14
这不容易,但我发现了。问题是,在我的JUnit测试中,我创建了如下服务器:
HttpServer server = HttpServerFactory.create(url);
但是这样,您就创建了一个轻量级容器,它没有servlet容器,失败的原因也是如此。因此,为了实现这一切,我使用了jersey测试框架,该框架允许使用Grizzly web容器(甚至嵌入glassfish)
以下是maven:
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<!-- Unit test are using jersey server directly -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.test.framework</groupId>
<artifactId>jersey-test-framework</artifactId>
<version>1.0.3</version>
<scope>test</scope>
</dependency>
</dependencies>
最后是资源文件,它包含@GET和@DELETE
@Path("root")
public class JaxRsController extends JaxRsResourceController{
List<String> peoples = new ArrayList<String>();
@GET
public String hello(){
System.out.println("Uri is "+getUri());
return "Hello "+peoples;
}
@DELETE
@Path("{name}")
public String deletePeople(@PathParam("name") String name){
System.out.println("deleting "+name);
this.peoples.remove(name);
return String.valueOf(peoples.size());
}
}
@Path(“根”)
公共类JaxRsController扩展了JAXRResourceController{
List people=new ArrayList();
@得到
公共字符串hello(){
System.out.println(“Uri是”+getUri());
返回“你好”+人;
}
@删除
@路径(“{name}”)
公共字符串deletePeople(@PathParam(“name”)字符串名){
System.out.println(“删除”+名称);
本.人名.删除(姓名);
返回字符串.valueOf(peoples.size());
}
}
现在它工作了!
我得到了一些帮助,还有一个。Beeing能够附加Jersey框架的源代码真的很有帮助,因此thantks也能连接到IntelliJ。它对我来说没用。这是我的答案。
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<!-- Unit test are using jersey server directly -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.test.framework</groupId>
<artifactId>jersey-test-framework</artifactId>
<version>1.0.3</version>
<scope>test</scope>
</dependency>
</dependencies>
public class JerseyServerTest extends JerseyTest {
protected String baseUri = "http://localhost:" + TestConstants.JERSEY_HTTP_PORT + "/";
public JerseyServerTest() throws Exception {
super("com.robustaweb.library.rest.controller");
/*
It's possible to NOT call the super() but to manually do :
1) ApplicationDescriptor appDescriptor = new ApplicationDescriptor()
.setRootResourcePackageName(resourcePackageName) // resource packages
.setContextPath(contextPath) //context of app
.setServletPath(servletPath); // context of spi servlet
2)setupTestEnvironment(appDescriptor);
*/
}
@Test
public void testHelloWorldRequest() {
SunRestClient client = new SunRestClient(baseUri + "root");
String result = client.GET("", null);
System.out.println(result);
}
@Test
public void testDeleteRequest() {
SunRestClient client = new SunRestClient(baseUri + "root");
String result = client.DELETE("john", null);
System.out.println(result);
}
}
@Path("root")
public class JaxRsController extends JaxRsResourceController{
List<String> peoples = new ArrayList<String>();
@GET
public String hello(){
System.out.println("Uri is "+getUri());
return "Hello "+peoples;
}
@DELETE
@Path("{name}")
public String deletePeople(@PathParam("name") String name){
System.out.println("deleting "+name);
this.peoples.remove(name);
return String.valueOf(peoples.size());
}
}