2012-10-23 2 views
6

JunitTest 시작 부분에 독립 실행 형 저어 서버가 실행되고 있습니다. 내 JaxRS controller이 작동하는지, 내 사용자 지정 HttpClient인지 테스트하고 있습니다. 글래스 피쉬에 포함 된이 JaxRsResourceController를 항상 사용할 수있었습니다.저지로 HttpServletRequest에 대한 종속성이 누락되었습니다.

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) 

을 기본적으로는에 말한다 : 여기

는 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; 
    } 

} 

나는 고객과 아무 문제가 없지만, 내가 서버를 시작할 때, 내가 가진이 @Context injection time, HttpServletRequest에 대한 의존성은 없다. 그러나 요청 및 응답시 @Context 주석을 제거했지만 그 내용을 UriInfo context으로 유지하면 문제가 없으며 Uri를 읽을 수 있습니다.

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

어떤 생각 :

은 내가 메이븐 치어 느릅 나무의 libs가 강제로 지금 몇 번 변경?

답변

1

쉬운 일이 아니지만 알아 냈습니다. 것은 내 JUnit 테스트에서, 나는이 같은 서버에 생성 된 것입니다 :

HttpServer server = HttpServerFactory.create(url); 

그러나 그런 식으로, 당신은 서블릿 컨테이너가없는 경량의 컨테이너를 생성, 그래서 실패 이유입니다. 따라서 모든 기능을 갖추기 위해 Grizzly 웹 컨테이너 (또는 심지어 내장 된 glassfish)를 사용할 수있는 저지 테스트 프레임 워크를 사용했습니다. @GET가 포함되어이

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

그리고 마지막으로 리소스 파일 JerseyTest

확장 점에 유의하고, @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()); 
    } 
} 
:

여기
<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입니다 : 여기

은 받는다는입니다

이제 작동합니다! this article에 도움이되었으므로 small chapter on the documentation이 있습니다. Beeing은 Jersey 프레임 워크의 소스 코드를 첨부 할 수있어서 정말 도움이되었으므로 IntelliJ에도 도움이되었습니다.

1

서블릿 종속성이 다른 모듈에 분리하여 치어에

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

를 추가하려고합니다.

+0

그것은 나를 위해 일하지 않았다. 여기 내 대답이다. –

관련 문제