如何使用RESTEasy客户端API处理嵌套MultipartInput时抛出的异常?

我已经构建了以下JAX-RS资源,该资源生成所需的响应对象,该对象为 multipart / mixed 与嵌套的 multipart / related (目前仅一个)嵌套的 application / json

stream()

此资源由使用RESTEasy client API via RESTEasy proxy framework构建的客户端消耗。 Response的实体被读取为MultipartInput。现在每个部分的正文都应读取为MultipartRelatedInput。与this is not possible (you are welcome to vote for)一样,解决方法是将每个零件的主体都读取为MultipartInput

@GET
@Path("test")
@Produces("multipart/mixed")
public Response test() {
  List<Entity> entities;
  ...
  final MultipartOutput multipartOutput = new MultipartOutput();
  entities.stream().map(entity -> {
    MultipartRelatedOutput multipartRelatedOutput = new MultipartRelatedOutput();
    multipartRelatedOutput.addPart(entity,MediaType.APPLICATION_JSON_TYPE);
    return multipartRelatedOutput;
  }).forEach(multipartRelatedOutput -> 
    multipartOutput.addPart(multipartRelatedOutput,MediaType.valueOf("multipart/related"))
  );
  return Response.ok(multipartOutput,MediaType.valueOf("multipart/mixed")).build();
}

将封装的实体读取为JSON-String(为简单起见,但也将其读取为具有实体类类型的对象),但发生以下异常:

public void get()
{
  try (Response response = this.proxy.test();) {
    ...
    MultipartInput multipartInput = response.readEntity(MultipartInput.class);
    for (InputPart inputPart : multipartInput.getParts()) {
    try {
      //MultipartRelatedInput relatedInput = inputPart.getBody(MultipartRelatedInput.class,null); // not working with NPE
      MultipartInput relatedInput = inputPart.getBody(MultipartInput.class,null);
      String json = relatedInput.getParts().get(0).getBodyAsString(); // fails
      // Entity entity = relatedInput.getParts().get(0).getBody(Entity.class,null); // also fails
    } catch (IOException exception) {}
  }
}

调用 ContextParameterInjector $ GenericDelegatingProxy.invoke 时,ResteasyProviderFactorycontextualData为空。

该如何解决?这是错误吗?

有趣的是,如果我将客户端方法本身设为JAX-RS资源,并从另一个客户端请求它,那么一切正常。 contextualData人口稠密。

Caused by: org.jboss.resteasy.spi.LoggableFailure: RESTEASY003880: Unable to find contextual data of type: javax.ws.rs.ext.Providers
    at org.jboss.resteasy.resteasy-jaxrs@3.7.0.Final//org.jboss.resteasy.core.ContextParameterInjector$GenericDelegatingProxy.invoke(ContextParameterInjector.java:77)
    at javax.ws.rs.api@1.0.2.Final//com.sun.proxy.$Proxy145.getMessageBodyReader(Unknown Source)
    at org.jboss.resteasy.resteasy-multipart-provider@3.7.0.Final//org.jboss.resteasy.plugins.providers.multipart.MultipartInputImpl$PartImpl.getBody(MultipartInputImpl.java:336)
    at org.jboss.resteasy.resteasy-multipart-provider@3.7.0.Final//org.jboss.resteasy.plugins.providers.multipart.MultipartInputImpl$PartImpl.getBodyAsString(MultipartInputImpl.java:392)

但是,如果直接调用客户端方法,仍然会出现同样的问题。

环境为Windows 10 Pro 1903,OpenJDK11U-jdk_x64_windows_hotspot_11.0.4_11,WildFly 17.0.1.Final,RESTEasy 3.7.0.Final。

nongdada123456 回答:如何使用RESTEasy客户端API处理嵌套MultipartInput时抛出的异常?

基于链接https://docs.jboss.org/resteasy/docs/3.0-beta-3/userguide/html/RESTEasy_Client_Framework.html 您可以看到报价

Resteasy将自动加载一组默认提供程序。 (基本上所有 META-INF/services/javax.ws.rs.ext.Providers个文件)。另外,你 可以手动注册其他提供程序,过滤器和拦截器 通过方法调用提供的Configuration对象 Client.configuration()Configuration还可以让您设置各种 可能需要的配置属性。

因此,如果您添加类名org.jboss.resteasy.plugins.providers.multipart.MultipartReader就足够了

本文链接:https://www.f2er.com/3143989.html

大家都在问