【问题标题】:How to make resource test with Multipart feature如何使用 Multipart 功能进行资源测试
【发布时间】:2016-05-16 21:10:46
【问题描述】:

我创建了我的资源来处理一些图像,我想用 JUnit 的@ClassRule 测试它们,就像我以前一样。它们看起来像这样:

@Path("/myImage")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postImage(
       @FormDataParam("file") InputStream inputStream) {
//doStuff
}

现在,我想对其进行测试,但遇到了问题。我争取这个班级规则会好的

@ClassRule
public static final ResourceTestRule resources = ResourceTestRule.builder()
                .addResource(new MyResource())
                .addResource(new MultiPartBundle())
                .build();

但我仍然收到错误

org.glassfish.jersey.server.model.ModelValidationException:应用程序资源模型的验证在应用程序初始化期间失败。

[[致命] 没有找到公共类型参数的注入源 ...

如何为这个问题写一个合适的类规则?

【问题讨论】:

    标签: java jersey multipartform-data dropwizard multipart


    【解决方案1】:

    这个错误是因为你没有在服务器上注册MutliPartFeatureMultiPartBundle(注册MultiPartFeature)不支持ResourceTestRule。所以你只需要自己注册就行了

    public static final ResourceTestRule resources = ResourceTestRule.builder()
                    .addResource(new MyResource())
                    .addProvider(MultiPartFeature.class) 
                    .build();
    

    与客户端相同。如果您想在客户端使用多部分序列化,您还需要注册该功能

    resource.client().register(MultiPartFeature.class)..
    

    你可以看到一个完整的例子here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 2019-07-05
      • 2015-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多