我在JunitTest的开头运行了一个Standalone Jersey服务器.我正在测试我的JaxRS控制器是否正常工作,以及我的自定义HttpClient.请注意,我一直能够使用嵌入在glassfish中的这个JaxRsResourceController.
这是JaxRsController(轻量版)
@Path("root")
public class JaxRsResourceController implements
ResourceController<HttpServletRequest> {
@Context
private UriInfo context;
@Context
HttpServletRequest request;
@Context
HttpServletResponse response;
@GET
public String hello(){
System.out.println("Uri is "+this.context.getBaseUri().toString());
return "Hello "+peoples;
}
}
Run Code Online (Sandbox Code Playgroud)
我对客户端没有问题,但是当我启动服务器时,我有:
GRAVE: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for field: javax.servlet.http.HttpServletRequest com.robustaweb.library.rest.controller.implementation.JaxRsResourceController.request
SEVERE: Missing dependency for field: javax.servlet.http.HttpServletResponse com.robustaweb.library.rest.controller.implementation.JaxRsResourceController.response
at com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:172)
at com.robustaweb.library.rest.server.JerseyServer.startServer(JerseyServer.java:44)
Run Code Online (Sandbox Code Playgroud)
基本上它表示在@Context注入时,不依赖于HttpServletRequest.但是,如果我在请求和响应中删除@Context注释,但保留它UriInfo context
,它没关系,我可以阅读Uri.
我改变了几次Maven pom,现在强制libs:
<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>
Run Code Online (Sandbox Code Playgroud)
任何的想法 ?
这并不容易,但我发现了。问题是,在我的 JUnit 测试中,我创建的服务器如下:
HttpServer server = HttpServerFactory.create(url);
Run Code Online (Sandbox Code Playgroud)
但那样的话,你创建的轻量级容器没有servlet容器,失败的原因也是如此。因此,为了拥有这一切,我使用了 jersey-test-framework,它允许使用 Grizzly Web 容器(甚至嵌入式 glassfish)。
这是行家:
<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>
Run Code Online (Sandbox Code Playgroud)
这是 JerseyServerTest :注意它扩展了 JerseyTest
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);
}
}
Run Code Online (Sandbox Code Playgroud)
最后是资源文件,包含@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());
}
}
Run Code Online (Sandbox Code Playgroud)
现在可以了!我在这篇文章中得到了一些帮助,并且有一个关于文档的小章节。能够附加 Jersey 框架的源代码确实很有帮助,所以也感谢 IntelliJ。
归档时间: |
|
查看次数: |
8130 次 |
最近记录: |