Reputation: 12039
I'm writing a JAX-RS service using Quarkus. My service should create a simple user profile with name, date of birth, etc. along with an upload image file. I'm trying to do this as a multipart form upload, but keep running into errors.
My service definition looks something like this...
@Path("/profile")
@POST
@Consumes({MediaType.MULTIPART_FORM_DATA})
@Produces({ MediaType.APPLICATION_JSON })
public Response createStudentProfile(@MultipartForm ProfileFormBean profile);
My ProfileFormBean
class looks like this...
public class ProfileFormBean {
@FormParam("firstName")
@PartType(MediaType.TEXT_PLAIN)
private String firstName = null;
@FormParam("lastName")
@PartType(MediaType.TEXT_PLAIN)
private String lastName = null;
@FormParam("profileImage")
@PartType(MediaType.APPLICATION_OCTET_STREAM)
private byte[] profileImage = null;
...
}
As best I can tell, this should be enough for my service to accept file uploads. I'm trying to test this out using curl, but when I do so I get an exception. My curl command is just trying to upload the file itself, not setting the first/last name properties:
curl -vki -H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H 'Accepts: application/json' \
-F 'profileImage=@profile_image.jpg' \
http://localhost:8081/profile
The output to this command looks like this:
curl -vki -H "Authorization: Bearer ${ACCESS_TOKEN}" \
> -H 'Accepts: application/json' \
> -F 'profileImage=@profile_image.jpg' \
> http://localhost:8081/profile
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8081 (#0)
> POST /profile HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/7.54.0
> Accept: */*
> Authorization: Bearer eyJhbGciOiJSUzI1N...
> Accepts: application/json
> Content-Length: 11701
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------49161ebc82f19511
>
< HTTP/1.1 415 Unsupported Media Type
HTTP/1.1 415 Unsupported Media Type
< Content-Length: 0
Content-Length: 0
* HTTP error before end of send, stop sending
<
* Closing connection 0
And on the server when I run this I see the following Exception:
15:26:35 DEBUG [org.jboss.resteasy.resteasy_jaxrs.i18n] RESTEASY002305: Failed executing POST /profile: javax.ws.rs.NotSupportedException: RESTEASY003065: Cannot consume content type
at org.jboss.resteasy.core.registry.SegmentNode.match(SegmentNode.java:453)
at org.jboss.resteasy.core.registry.SegmentNode.match(SegmentNode.java:155)
at org.jboss.resteasy.core.registry.RootNode.match(RootNode.java:58)
at org.jboss.resteasy.core.registry.RootClassNode.match(RootClassNode.java:47)
at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:482)
at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:323)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:244)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:153)
at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:363)
at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:156)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:238)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:249)
at io.quarkus.resteasy.runtime.ResteasyFilter.doFilter(ResteasyFilter.java:30)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:63)
at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:133)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:65)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:270)
at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:59)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:116)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:113)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at io.quarkus.undertow.runtime.UndertowDeploymentRecorder$9$1$1.call(UndertowDeploymentRecorder.java:476)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:250)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:59)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:82)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:290)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:669)
at io.quarkus.runtime.CleanableExecutor$CleaningRunnable.run(CleanableExecutor.java:224)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2011)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1535)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1395)
at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
at java.base/java.lang.Thread.run(Thread.java:830)
at org.jboss.threads.JBossThread.run(JBossThread.java:479)
Am I missing something? I found documentation on the Quarkus site for creating REST clients with multipart, but nothing for server-side resources. Is there something more I need to do to get this working?
Upvotes: 3
Views: 6686
Reputation: 208944
The problem is with
@FormParam("profileImage")
@PartType(MediaType.APPLICATION_OCTET_STREAM)
private byte[] profileImage = null;
You are advertising that the only content-type you are allowing for this part is application/octet-stream
. But you are sending an image, and cURL is not setting the content-type of the image to that. I would imagine under the hood it is setting it to image/jpg
. You could manually set the type (and that will fix this request)
profileImage=@profile_image.jpg;type=application/octet-stream
But personally, I would just remove the @PartType
. Since the type of the field is already byte[]
, there will be no problems with conversion. Also, a lot of clients are not able to set individual parts' content-type. Like browser/JS clients will default to setting the content-type for files, just like cURL did.
Upvotes: 5