2012-10-23 13 views
6

JunitTestの開始時にスタンドアロンジャージーサーバーを実行しています。 JaxRS controllerが動作しているかどうか、私のカスタムHttpClientをテストしています。私はいつもglassfishに埋め込まれたこのJaxRsResourceControllerを使用することができました。ここでJerseyで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のウィッヒがで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> 

任意のアイデアを?

答えて

1

これは簡単ではありませんでしたが、わかりました。事は私のJUnitテストでは、私はこのようなサーバー作成していたということです。

HttpServer server = HttpServerFactory.create(url); 

しかし、そのように、あなたはサーブレットコンテナを持たない軽量コンテナを作成し、その失敗の理由があります。ですから、私は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> 

は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があります。 Jerseyフレームワークのソースコードを添付できるBeeingは本当に助けになりました。IntelliJもそうです。

1

サーブレットの依存関係を別のモジュールに分離して、あなたのポンポンに

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

を追加してみてください。

+0

それは私の仕事ではありませんでした。ここに私の答えがあります。 –

関連する問題