【问题标题】:"Unable to find a MessageBodyReader" error with multipart request containing a file and JSON包含文件和 JSON 的多部分请求出现“无法找到 MessageBodyReader”错误
【发布时间】:2020-04-23 11:43:56
【问题描述】:

我有一个简单的 Quarkus 应用程序,它有一个 POST 资源。

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response updateContent(@MultipartForm MyRequest request) {
    bus.sendAndForget("request", request);
    return Response.accepted().build();
}

MyRequest 看起来像这样:

public class MyRequest {
  @FormParam("template")
  @PartType(MediaType.APPLICATION_OCTET_STREAM)
  private byte[] template;

  @FormParam("data")
  @PartType(MediaType.TEXT_PLAIN)
  private Map<String, String> data;

  // Default constructor & getters
}

然后我用 Postman 发送以下内容:

然后我得到以下错误:

java.lang.RuntimeException: RESTEASY007545: Unable to find a MessageBodyReader for media type: text&#x2F;plain; charset=us-ascii and class type java.util.Map

如果我只发送模板,它可以工作,所以似乎由于某种原因无法解析 JSON 字符串。

我认为我拥有所有必需的依赖项,例如:

  • quarkus-resteasy-杰克逊
  • resteasy-multipart-provider
  • resteasy-jackson2-provider

我还尝试手动注册 ResteasyJackson2Provider,并将数据属性的 mediaType 更改为 APPLICATION_JSON,但这没有帮助。我错过了什么,或者我什至正确地发送了 JSON?

【问题讨论】:

    标签: java deserialization quarkus


    【解决方案1】:

    对于表单数据,只有文件和文本字段。因此,任何类型仍将被解释为表单数据中的文本/纯文本。但是,在请求过滤器继续解析数据之前,您可以在请求过滤器中设置一个参数。

    创建ContainerRequestFilter的实例:

    @ApplicationScoped
    @Provider
    public class MyFilter implements ContainerRequestFilter {
        @Override
        public void filter(ContainerRequestContext requestContext) throws IOException {
            // apply next only for your form-data path and ignore all the other requests
            final HttpRequest httpRequest = ResteasyContext.getContextData(HttpRequest.class);
            httpRequest.setAttribute("resteasy.provider.multipart.inputpart.defaultContentType", "application/json");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-24
      • 2019-06-28
      • 2013-05-03
      • 2017-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多