使用Jersey缺少HttpServletRequest的依赖项

时间:2012-10-23 22:28:00

标签: jersey

我在JunitTest的开头运行了一个Standalone Jersey服务器。我正在测试我的JaxRS controller是否正常,以及我的自定义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;
    }

}

我对客户端没有问题,但是当我启动服务器时,我有:

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)

基本上它表示在@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>

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

将servlet依赖项分离到另一个模块,尝试添加

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.14</version>
</dependency>

到你的pom。

答案 1 :(得分:1)

这并不容易,但我发现了。问题是,在我的JUnit测试中,我正在创建这样的服务器:

HttpServer server = HttpServerFactory.create(url);

但是这样,你创建了一个没有servlet容器的轻量级容器,失败的原因也是如此。因此,为了拥有这一切,我使用了允许使用Grizzly Web容器(甚至是嵌入式玻璃鱼)的泽西测试框架。

这是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>

这是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);
    }
}

最后是资源文件,包含@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());
    }
}

现在它有效! 我在this article得到了一些帮助,并且有一个small chapter on the documentation。 Beeing能够附加Jersey框架的源代码确实有帮助,所以也可以使用IntelliJ。