繁体   English   中英

在com.sun.jersey.guice.spi.container.servlet.GuiceContainer中找不到合适的构造函数

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

我是Guice的新手。 我想使用RESTful Web服务和Guice for DI创建服务器应用程序。 我在这里按照教程进行操作,而不是使用Tomcat6作为码头。 但我不能让它运行:(

我总是在应用程序启动时在com.sun.jersey.guice.spi.container.servlet.GuiceContainer中找不到合适的构造函数

向类de.server.MyGuiceServletConfig com.google.inject.CreationException的侦听器实例发送上下文初始化事件的异常:Guice创建错误:1)在com.sun.jersey.guice.spi.container.servlet中找不到合适的构造函数。 GuiceContainer。 类必须具有用@Inject注释的一个(并且只有一个)构造函数或非私有的零参数构造函数。 在com.sun.jersey.guice.JerseyServletModule.webApp(JerseyServletModule.java:90)上的com.sun.jersey.guice.spi.container.servlet.GuiceContainer.class(GuiceContainer.java:108)

GuiceContainer类具有一个带有@Inject语句的构造函数,该构造函数需要一个Injector。

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

我的web.xml看起来像这样:

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

我的配置类如下所示:

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

最后但并非最不重要的其他服务:

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

我不知道我在想什么。 希望你能告诉我我做错了什么...

如果您需要更多信息,请发表评论,我会添加。

提前Thx,TJ

好的,我发现了问题。

听起来有些怪异,但是问题出在下面(我没有在问题中提到它,因为我从未暗示过可能是导致问题的构建过程):

我在pom中定义了一个姓氏:

<build>
<finalName>mytest</finalName>

而这正是问题所在。 当我删除此行时,一切正常。

有人可以告诉我为什么在打仗时输入姓氏会带来问题吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM