2012-10-23 10 views
6

Mam niezależny serwer Jersey uruchomiony na początku mojego JunitTest. Testuję, czy mój JaxRS controller działa, jak również mój niestandardowy HttpClient. Należy pamiętać, że zawsze byłem w stanie użyć tego JaxRsResourceController osadzonego w glassfish.Brakujące zależności dla HttpServletRequest z Jersey

Oto JaxRsController (wersja light)

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

} 

nie mam problemu z klientem, ale kiedy uruchomić serwer, mam:

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) 

Zasadniczo mówi, że na @Context czas wtrysku, nie ma zależności od HttpServletRequest. Jeśli jednak usuwam adnotacje @Context na żądanie i odpowiedź, ale zachowam je dla UriInfo context, jest OK, i mogę przeczytać Uri.

zmieniłem kilka razy pom wich Maven jest teraz zmusić bibliotekami w:

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

jakiś pomysł?

Odpowiedz

1

To nie było łatwe, ale się dowiedziałem. Chodzi o to, że w moim teście JUnit, byłem tworzenia serwera takiego:

HttpServer server = HttpServerFactory.create(url); 

ale sposób można utworzyć lekki pojemnik, który nie ma kontenerów serwletów, a więc jest powodem awarii. Aby mieć wszystko, skorzystałem z platformy testowej koszulki, która pozwala używać kontenera internetowego Grizzly (a nawet osadzonego glassfisha).

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

Oto JerseyServerTest: zauważ, że rozciąga 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); 
    } 
} 

I wreszcie pliku zasobów, który zawiera @GET i @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()); 
    } 
} 

A teraz działa! Mam pomoc w this article, a tam jest small chapter on the documentation. Beeing w stanie dołączyć kod źródłowy z frameworkiem Jersey naprawdę pomógł, więc również do IntelliJ.

1

serwletów zależności rozdzielono do innego modułu, spróbuj dodać

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

do pom.

+0

To nie wykonało się dla mnie. Oto moja odpowiedź. –

Powiązane problemy