2012-10-23 47 views
6

我有一個獨立的澤西服務器運行在我的JunitTest的開始。我測試如果我的JaxRS controller工作,以及我的自定義HttpClient。請注意,我一直可以使用嵌入glassfish中的JaxRsResourceController。與澤西島缺少HttpServletRequest的依賴關係

這裏是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> 

任何想法?

回答

1

這並不容易,但我發現了。問題是,在我的JUnit測試,我創建這樣的服務器:

HttpServer server = HttpServerFactory.create(url); 

但這樣一來,你創建一個沒有servlet容器一個輕量級的容器,所以是失敗的原因。所以爲了實現這一切,我使用了允許使用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> 

這裏是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能夠附加澤西框架的源代碼真的有幫助,所以也適用於IntelliJ。

1

的servlet依賴分離到另一個模塊,嘗試添加

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

你的POM。

+0

它沒有爲我做這項工作。這是我的答案。 –