Rest 在JerseyTest中找不到自定义MessageBodyReader

Rest 在JerseyTest中找不到自定义MessageBodyReader,rest,glassfish,jersey,jax-rs,jersey-2.0,Rest,Glassfish,Jersey,Jax Rs,Jersey 2.0,我的球衣测试课有一个奇怪的问题 当执行测试代码并在的第203行上放置断点时,我看到我的reader不在reader.workers中。但是,正如您在下面看到的,我在ResourceConfig中注册了这个MessageBodyReader 下面提供了所有相关代码 我的自定义MessageBodyReader/Writer (从中拉出。) 它正在测试的资源。。。 (请注意,@GET等web服务注释位于接口中。) 生成此失败跟踪 org.glassfish.jersey.message.intern

我的球衣测试课有一个奇怪的问题

当执行测试代码并在的第203行上放置断点时,我看到我的reader不在reader.workers中。但是,正如您在下面看到的,我在ResourceConfig中注册了这个MessageBodyReader

下面提供了所有相关代码

我的自定义MessageBodyReader/Writer (从中拉出。)

它正在测试的资源。。。 (请注意,@GET等web服务注释位于接口中。)

生成此失败跟踪 org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: 找不到媒体的MessageBodyReader type=application/vnd.com.company-v1+json,type=class com.company.rest.v1.resources.location.json.LocationListV1, genericType=类 com.company.rest.v1.resources.location.json.LocationListV1。 在 org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:207) 在 org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.procedue(ReaderInterceptorExecutor.java:139) 在 org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1109) 在 org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:851) 在 org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:785) 在 org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(inboundjaxresponse.java:96) 在 org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:761) 在 org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:90) 在 org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:671) 位于org.glassfish.jersey.internal.Errors.process(Errors.java:315) org.glassfish.jersey.internal.Errors.process(Errors.java:297)位于 org.glassfish.jersey.internal.Errors.process(Errors.java:228)位于 org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:422) 在 org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:667) 在 org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:396) 在 org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:296) 在 com.company.rest.resources.location.TestLocationResource.basicTest(TestLocationResource.java:47) [……]

... 使用此控制台输出 接着是这个,暗示有一个成功的反应
2013年10月29日下午3:30:22 org.glassfish.jersey.filter.LoggingFilter log
信息:2*LoggingFilter-在主线程上收到响应
2 < 200
日期:2013年10月29日星期二格林尼治标准时间22:30:21
2<内容长度:16
2<内容类型:application/vnd.com.company-v1+json
{“地点”:[]}
2013年10月29日下午3:30:22 org.glassfish.jersey.test.grizzly.GrizzlyTestContainer工厂$GrizzlyTestContainer站
信息:正在停止GrizzlyTestContainer。。。
2013年10月29日下午3:30:22 org.glassfish.grizzly.http.server.NetworkListener停止
信息:已停止绑定到[localhost:9998]的侦听器

有人知道我做错了什么吗?

第一个堆栈跟踪来自您的客户端,因为您没有在那里注册消息正文提供程序(因此无法找到)。方法应该仅用于配置服务器端。还有另一个名为的方法,用于客户端。如果要使用自定义提供程序,则需要重写这两个方法

第二个堆栈跟踪来自
位置ResourceHK2Binder
。借

bindAsContract(LocationDao.class).in(Singleton.class);
您告诉HK2应该将
LocationDao
类实例化为单例,然后HK2将其注入
LocationDao
类型。您可能需要更改活页夹以使用以下内容:

bind(new FakeLocationDao()).to(LocationDao.class);

有关此主题的更多信息,请参阅。

我可能错了,但这看起来像是一个Jersey bug。当我执行
client().register(JsonMessageBodyHandlerV1.class)时
configure()
之外(例如在
@Test
内或在
之前的自定义
@中),它可以工作。仍然不知道如何摆脱那些垃圾邮件“找不到合适的构造函数”的错误消息。谢谢Michal,我不知道configureClient。就您的binder注释而言,这--
bind(new FakeLocationDao()).to(LocationDao.class).in(Singleton.class)
是不可能被他们的API实现的,因为第一个
bind(T服务)
返回一个
ScopedBindingBuilder
,它不允许您以我设置的方式指定一个合同(在
中)但它只是在控制台警告消息中垃圾邮件异常。我查看了您提供的链接,没有看到如何解决这个问题。也就是说,
bind(mockedDaoInstance).to(LocationDao.class).to(new-TypeLiteral(){})与上面的效果相同。哦$h,我错了。我在运行整个测试套件,但仍然看到异常——愚蠢的我。我上面评论中的那句话起作用了。谢谢,先生:)好的,在(…)
呼叫中删除
。当您绑定一个实例时,根据定义,它在单例范围内。我有罪。
public class LocationResourceV1 implements ILocationResourceV1 {
  ...

  @Inject
  private LocationDao daoLoc;

  private final LocationTranslator translator = new LocationTranslator();

  @Override
  public LocationListV1 listV1(String apiKey) {
    return translator.translate(daoLoc.query(LocationFilters.SELECT_ALL));
  }

  ...

  @VisibleForTesting
  public void setLocationDao(LocationDao dao) {
    this.daoLoc = dao;
  }
}
[...]
INFO: [HttpServer] Started.
Oct 29, 2013 4:26:16 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * LoggingFilter - Request received on thread main
1 > GET http://localhost:9998/location/list
1 > Accept: application/vnd.com.company-v1+json
1 > X-ApiKey: abcdefg

Oct 29, 2013 3:30:21 PM org.glassfish.jersey.internal.Errors logErrors
WARNING: The following warnings have been detected: WARNING: HK2 service reification failed for [com.company.persistence.dao.intf.LocationDao] with an exception:
MultiException stack 1 of 2
java.lang.NoSuchMethodException: Could not find a suitable constructor in com.company.persistence.dao.intf.LocationDao class.
    at org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getConstructor(JerseyClassAnalyzer.java:189)
    at org.jvnet.hk2.internal.Utilities.getConstructor(Utilities.java:159)
    at org.jvnet.hk2.internal.ClazzCreator.initialize(ClazzCreator.java:125)
    at org.jvnet.hk2.internal.ClazzCreator.initialize(ClazzCreator.java:176)
    at org.jvnet.hk2.internal.SystemDescriptor.internalReify(SystemDescriptor.java:649)
    at org.jvnet.hk2.internal.SystemDescriptor.reify(SystemDescriptor.java:604)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.reifyDescriptor(ServiceLocatorImpl.java:396)
    [...]
MultiException stack 2 of 2
java.lang.IllegalArgumentException: Errors were discovered while reifying SystemDescriptor(
    implementation=com.company.persistence.dao.intf.LocationDao
    contracts={com.company.persistence.dao.intf.LocationDao}
    scope=org.glassfish.jersey.process.internal.RequestScoped
    qualifiers={}
    descriptorType=CLASS
    descriptorVisibility=NORMAL
    metadata=
    rank=0
    loader=org.glassfish.hk2.utilities.binding.AbstractBinder$2@568bf3ec
    proxiable=null
    proxyForSameScope=null
    analysisName=null
    id=143
    locatorId=0
    identityHashCode=2117810007
    reified=false)
    at org.jvnet.hk2.internal.SystemDescriptor.reify(SystemDescriptor.java:615)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.reifyDescriptor(ServiceLocatorImpl.java:396)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.narrow(ServiceLocatorImpl.java:1916)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.access$700(ServiceLocatorImpl.java:113)
    at org.jvnet.hk2.internal.ServiceLocatorImpl$6.compute(ServiceLocatorImpl.java:993)
    at org.jvnet.hk2.internal.ServiceLocatorImpl$6.compute(ServiceLocatorImpl.java:988)
    [...]
    [...]
    (Above is repeated 4 times)
    [...]
    [...]
Oct 29, 2013 3:30:22 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 2 * LoggingFilter - Response received on thread main
2 < 200
2 < Date: Tue, 29 Oct 2013 22:30:21 GMT
2 < Content-Length: 16
2 < Content-Type: application/vnd.com.company-v1+json
{"locations":[]}

Oct 29, 2013 3:30:22 PM org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory$GrizzlyTestContainer stop
INFO: Stopping GrizzlyTestContainer...
Oct 29, 2013 3:30:22 PM org.glassfish.grizzly.http.server.NetworkListener stop
INFO: Stopped listener bound to [localhost:9998]
bindAsContract(LocationDao.class).in(Singleton.class);
bind(new FakeLocationDao()).to(LocationDao.class);