"Unable to find a MessageBodyReader" 包含文件的多部分请求出错并且 JSON
"Unable to find a MessageBodyReader" error with multipart request containing a file and JSON
我有一个简单的 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/plain; charset=us-ascii and class type java.util.Map
如果我只发送模板,它可以工作,所以似乎由于某种原因无法解析 JSON 字符串。
我想我拥有所有必需的依赖项,例如:
- quarkus-resteasy-jackson
- resteasy-multipart-provider
- resteasy-jackson2-provider
而且我也尝试过手动注册ResteasyJackson2Provider,并将数据的mediaType属性更改为APPLICATION_JSON,但没有帮助。我错过了什么,或者我是否正确发送了 JSON?
对于表单数据,只有文件和文本字段。因此,任何类型在表单数据中仍将被解释为 text/plain。但是,在继续解析数据之前,您可以在请求过滤器中设置一个参数。
创建 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");
}
}
我有一个简单的 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/plain; charset=us-ascii and class type java.util.Map
如果我只发送模板,它可以工作,所以似乎由于某种原因无法解析 JSON 字符串。
我想我拥有所有必需的依赖项,例如:
- quarkus-resteasy-jackson
- resteasy-multipart-provider
- resteasy-jackson2-provider
而且我也尝试过手动注册ResteasyJackson2Provider,并将数据的mediaType属性更改为APPLICATION_JSON,但没有帮助。我错过了什么,或者我是否正确发送了 JSON?
对于表单数据,只有文件和文本字段。因此,任何类型在表单数据中仍将被解释为 text/plain。但是,在继续解析数据之前,您可以在请求过滤器中设置一个参数。
创建 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");
}
}