首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义MessageBodyReader在JerseyTest中找不到

自定义MessageBodyReader在JerseyTest中找不到
EN

Stack Overflow用户
提问于 2013-10-29 22:55:44
回答 1查看 3.1K关注 0票数 1

我的JerseyTest课程有个奇怪的问题。

当执行我的测试代码并在org.glassfish.jersey.message.internal.ReaderInterceptorExecutor的第203行放置一个断点时,我看到我的阅读器不在reader.workers中。但是,正如您在下面看到的,我在MessageBodyReader中注册了这个ResourceConfig。

所有相关代码如下所示。

我的定制MessageBodyReader/Writer

代码语言:javascript
复制
@Provider
@Produces({V1_JSON})
@Consumes({V1_JSON})
public class JsonMessageBodyHandlerV1
    implements
      MessageBodyWriter<Object>,
      MessageBodyReader<Object> {
  ...
}

是的,isReadable返回true

调试时,我看到代码击中了writeTo,而不是readFrom

失败的测试代码

代码语言:javascript
复制
public class TestLocationResource extends JerseyTest {

  public static class LocationResourceHK2Binder extends AbstractBinder {

    @Override
    protected void configure() {
      // Singleton bindings.
      bindAsContract(LocationDao.class).in(Singleton.class);

      // Singleton instance bindings.
      bind(new FakeLocationDao()).to(LocationDao.class);
    }
  }

  @Test
  public void basicTest() {

    LocationListV1 actualResponse = /**/
    /**/target(LocationResourceV1.PathFields.PATH_ROOT)
    /*   */.path(LocationResourceV1.PathFields.SUBPATH_LIST)
    /*   */.request(V1_JSON)
    /*   */.header(HEADER_API_KEY, "abcdefg")
    /*   */.get(LocationListV1.class);

    assertEquals(10, actualResponse.getLocations().size());
  }

  @Override
  protected Application configure() {
    enable(TestProperties.LOG_TRAFFIC);
    enable(TestProperties.DUMP_ENTITY);

    ResourceConfig rc = new ResourceConfig();
    rc.registerClasses(LocationResourceV1.class, JsonMessageBodyHandlerV1.class);
    rc.register(new LocationResourceHK2Binder());

    return rc;
  }
}

(从这个例子.)

它测试的资源..。

代码语言:javascript
复制
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;
  }
}

(注意,诸如@GET之类的web服务注释在接口中。)

生成此故障跟踪。

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader未找到媒体类型=application/vnd.com.Company-v1+json,type=class com.company.rest.v1.resources.location.json.LocationListV1,genericType=class com.company.rest.v1.resources.location.json.LocationListV1.在org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:207) at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:139) at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1109) at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:851) at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:785)在org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:96) at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:761) at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:90) at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:671) at org.glassfish.jersey.internal.Errors.process(Errors.java:315) at org.glassfish.jersey.internal.Errorsorg.glassfish.jersey.internal.Errors.process(Errors.java:228) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:422) org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:667) at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:396) at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:296 .process(Errors.java:297))在com.company.rest.resources.location.TestLocationResource.basicTest(TestLocationResource.java:47) .

..。有了这个控制台输出

代码语言:javascript
复制
[...]
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)
    [...]
    [...]

接着是这个,暗示有一个成功的反应。

代码语言:javascript
复制
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]

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-30 10:25:22

第一个堆栈跟踪来自您的客户端,因为您没有在那里注册消息体提供程序(因此找不到它)。JerseyTest#configure方法应该只用于配置服务器端.还有一个名为JerseyTest#configureClient的方法,用于客户端。如果要使用自定义提供程序,则需要覆盖这两个方法。

第二个堆栈跟踪来自您的LocationResourceHK2Binder。通过

代码语言:javascript
复制
bindAsContract(LocationDao.class).in(Singleton.class);

您告诉HK2,应该将LocationDao类实例化为单例,然后HK2将其注入LocationDao类型。您可能希望更改您的活页夹,以使用以下内容:

代码语言:javascript
复制
bind(new FakeLocationDao()).to(LocationDao.class);

有关此主题的详细信息,请参阅自定义注入和生命周期管理

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19670692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档