6

我在 JunitTest 开始时运行了一个独立的 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;
    }

}

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

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,现在是强制库:

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

任何的想法 ?

4

2 回答 2

1

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

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

到你的 pom。

于 2012-10-24T05:52:05.293 回答
1

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

HttpServer server = HttpServerFactory.create(url);

但是这样一来,您创建了一个没有 servlet 容器的轻量级容器,失败的原因也是如此。所以为了拥有这一切,我使用了 jersey-test-framework 允许使用 Grizzly web 容器(甚至是 Embedded 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>

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

现在它起作用了!我在这篇文章中得到了一些帮助,并且有一个关于文档的小章节。Beeing 能够附加 Jersey 框架的源代码真的很有帮助,所以也感谢 IntelliJ。

于 2012-10-24T09:39:17.333 回答