TerenceJackson
TerenceJackson

Reputation: 1786

Could not find a suitable constructor in com.sun.jersey.guice.spi.container.servlet.GuiceContainer

I am new to Guice. I want to create a server application with RESTful web services and Guice for DI. I followed the tutorial here, instead of a jetty I use a Tomcat6. But I cannot get it to run :(

I always get Could not find a suitable constructor in com.sun.jersey.guice.spi.container.servlet.GuiceContainer on the application startup.

Exception sending context initialized event to listener instance of class de.server.MyGuiceServletConfig com.google.inject.CreationException: Guice creation errors: 1) Could not find a suitable constructor in com.sun.jersey.guice.spi.container.servlet.GuiceContainer. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private. at com.sun.jersey.guice.spi.container.servlet.GuiceContainer.class(GuiceContainer.java:108) at com.sun.jersey.guice.JerseyServletModule.webApp(JerseyServletModule.java:90)

The GuiceContainer class has an constructor with an @Inject statement where it wants an Injector.

  @Inject
public GuiceContainer(Injector injector) {
    this.injector = injector;
}

My web.xml looks like this:

<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>
<filter>
    <filter-name>guiceFilter</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>guiceFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
    <listener-class>de.server.MyGuiceServletConfig</listener-class>
</listener>

<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/rest</param-value>
</context-param>

<listener>
    <listener-class>
        org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
    </listener-class>
</listener>

<servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>
        org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    </servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping> 

My config class looks like this:

public class MyGuiceServletConfig extends GuiceServletContextListener {

public MyGuiceServletConfig() {

}

public class MyJerseyServletModule extends JerseyServletModule {
    @Override
    protected void configureServlets() {
        // Must configure at least one JAX-RS resource or the
        // server will fail to start.
        bind(ITest.class).to(Test2.class);  
        bind(TestRestService.class);

        serve("/*").with(GuiceContainer.class);
    }
}

@Override
protected Injector getInjector() {
    return Guice.createInjector(new MyJerseyServletModule());
}}

And last but not least the rest service:

@Path("/test")
public class TestRestService {

private ITest test;

@Inject
public TestRestService(ITest t){
    this.test = t;
}

@GET
@Path("/getMe")
public String getMe() {
    return test.getName();
}


@GET
@Path("/getAll")
public Response getAll() {
    return Response.status(200).entity("sadsads").build();
}}

I don't know what I am missing. Hope you can tell me what I did wrong...

If you need any further information, just leave a comment and I'll add it.

Thx in advance, TJ

Upvotes: 0

Views: 4445

Answers (1)

TerenceJackson
TerenceJackson

Reputation: 1786

Ok, I found the problem.

It sounds a little weird, but the problem is the following (I didn't mentioned it in my question, cause I never suggested that it could be the build process which causes the problems):

I defined a final name in my pom:

<build>
<finalName>mytest</finalName>

And this is exactly the problem. When I remove this line, everything works perfect...

Can someone tell me why this it is problematic to enter a final name when building a war?

Upvotes: 0

Related Questions