I'm noticing a weird issue with multipart request here.
Below is the Jersey2 implementation used in Spring Boot 2.4.2:
@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)
{
...
}
and following the Spring Boot dependencies:
<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>
I am able to successfully upload JSON and file (as multipart/form-data) using Postman but the same request from Java client throws below error:
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
This article on SO says we need to add a CommonsMultipartResolver
but why as it works all good from Postman client??
Appreciate any hints or suggestions, thanks.
Updating Java Apache client code:
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();