Guice,JerseyServletModule,缺少对方法的依赖,REST 服务配置
Posted
技术标签:
【中文标题】Guice,JerseyServletModule,缺少对方法的依赖,REST 服务配置【英文标题】:Guice, JerseyServletModule, missing dependency for method, REST service configuration 【发布时间】:2012-08-11 01:36:03 【问题描述】:我的 Guice 配置类似于:
public class SocialServiceGuiceConfig extends GuiceServletContextListener
@Override
protected Injector getInjector()
final Module [] modules =
new JerseyServletModule()
@Override
protected void configureServlets()
bind( GraphApi.class );
bind( SocialUncheckedExceptionMapper.class ).in( Singleton.class );
bind( SocialUnhandledExceptionMapper.class ).in( Singleton.class );
serve( "/*" ).with(
GuiceContainer.class );
,
;
return Guice.createInjector( modules );
我的 API 定义如下所示:
@Path( "/" + BUSINESS_CUSTOMER_PARAMETER + "" )
public BusinessCustomerApi businessCustomerAndContext(
@PathParam( BUSINESS_CUSTOMER_PARAMETER ) final BusinessCustomerPathParam businessCustomerPathParam,
@QueryParam( LOCALE_PARAMETER ) final String locale)
我在日志中遇到的异常:
INFO: Binding com.business.social.next.service.api.v1.GraphApi to GuiceManagedComponentProvider with the scope "PerRequest"
Aug 13, 2012 9:20:05 PM com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
WARNING: A HTTP GET method, public void com.business.social.next.service.api.v1.GraphApi.get(), MUST return a non-void type.
SEVERE: Missing dependency for method public com.business.social.next.service.api.v1.BusinessCustomerApi com.business.social.next.service.api.v1. GraphApi.graphRequestContext(com.business.social.next.graph.protocol.BusinessCustomer,java.lang.String) at parameter at index 0
Aug 13, 2012 9:20:05 PM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter Guice Filter
com.sun.jersey.spi.inject.Errors$ErrorMessagesException
at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771)
at com.sun.jersey.guice.spi.container.servlet.GuiceContainer.initiate(GuiceContainer.java:121)
at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)
at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at com.google.inject.servlet.ServletDefinition.init(ServletDefinition.java:117)
at com.google.inject.servlet.ManagedServletPipeline.init(ManagedServletPipeline.java:82)
at com.google.inject.servlet.ManagedFilterPipeline.initPipeline(ManagedFilterPipeline.java:102)
at com.google.inject.servlet.GuiceFilter.init(GuiceFilter.java:172)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3693)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1015)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1015)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Aug 13, 2012 9:20:05 PM org.apache.catalina.core.StandardContext start
SEVERE: Error filterStart
Aug 13, 2012 9:20:05 PM org.apache.catalina.core.StandardContext start
SEVERE: Context [] startup failed due to previous errors
为什么会这样?很难在网上找到信息,也想不出要找什么途径。
【问题讨论】:
【参考方案1】:原来我没有遵循BusinessCustomerPathParam
类型上的@PathParam
类型规则。
这里列出了规则:http://jsr311.java.net/nonav/releases/1.1/javax/ws/rs/PathParam.html
带注释的参数、字段或属性的类型必须是:
为PathSegment
,该值将是路径匹配部分的最后一段。有关检索所有请求路径段的方法,请参阅UriInfo
。 为List<PathSegment>
,值将是PathSegment
的列表,对应于与命名模板参数匹配的路径段。有关检索所有请求路径段的方法,请参阅UriInfo
。 是原始类型。 有一个接受单个字符串参数的构造函数。 有一个名为valueOf
或fromString
的静态方法,它接受单个字符串参数(例如,请参阅Integer.valueOf(String)
)。
我没有采用String
或static
选项之一的构造函数。
很遗憾,日志中提供的错误消息并不是很有用。
【讨论】:
【参考方案2】:我在尝试绑定具有注入参数的接口时收到了同样的消息,但是我忘记将 @Inject
注释添加到我的构造函数中。记得检查一下,因为它让我的头撞墙很久了。
【讨论】:
以上是关于Guice,JerseyServletModule,缺少对方法的依赖,REST 服务配置的主要内容,如果未能解决你的问题,请参考以下文章