SpringBoot2.x报告java.lang.illegalstateexception:无法处理部件,因为没有提供多部件配置

1tu0hz3e  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(661)

我注意到多部分请求有一个奇怪的问题。
下面是spring boot 2.4.2中使用的jersey2实现:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_JSON})
public void upload(@FormDataParam("params") MyPojo req,
        @FormDataParam("file") FormDataBodyPart file, @Context HttpHeaders headers, @Suspended AsyncResponse ar)
{
    ...
}

遵循spring引导依赖关系:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
</dependency>

我能够使用postman成功地上传json和文件(作为多部分/表单数据),但是来自java客户端的相同请求引发以下错误:

Caused by: org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided

这篇关于so的文章说我们需要添加一个 CommonsMultipartResolver 但是为什么 Postman 的客户都很好用??
感谢您的任何提示或建议,谢谢。
更新java apache客户端代码:

final Document document = getDocument(documentId);
        final String requestParams = getRequestParams(document);
        final String documentContentType = document.getContentType();
        final URL endpoint = getServiceEndpoint();

        final MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create().addTextBody(REQUEST_PARAMS_PARAMETER_NAME, requestParams, ContentType.APPLICATION_JSON);
        if (isSendDocument() && documentFile != null) {
            entityBuilder.addBinaryBody(DOCUMENT_CONTENT_PARAMETER_NAME, documentFile, ContentType.parse(documentContentType), document.getContentType());
        }

        final HttpEntity reqestEntity = entityBuilder.build();
        connection = (HttpURLConnection)endpoint.openConnection();
        connection.setAllowUserInteraction(false);
        connection.setConnectTimeout(getConnectionTimeout());
        connection.setReadTimeout(getReadTimeout());
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.addRequestProperty(X_REQUESTED_WITH_HEADER_NAME, "XMLHttpRequest");
        connection.addRequestProperty(X_REQUESTED_BY_HEADER_NAME, "XMLHttpRequest");
        connection.addRequestProperty(ACCEPT_HEADER_NAME, ContentType.APPLICATION_JSON.getMimeType());
        connection.addRequestProperty(CONTENT_TYPE_HEADER_NAME, reqestEntity.getContentType().getValue());
        outStream = connection.getOutputStream();
        reqestEntity.writeTo(outStream);
        outStream.flush();
qaxu7uf2

qaxu7uf21#

当我们打算让jersey servlet处理多部分时,@paulsamsotha建议禁用spring-boot的多部分处理 spring.servlet.multipart.enabled = false 解决了问题“java.lang.illegalstateexception:无法处理部件,因为没有提供多部件配置”中发布的原始问题。
后来,我只在apache客户机上收到了一个http400错误,这是由于json负载格式错误造成的。谢谢大家的支持。

相关问题