I want to create an employee information in the system by uploading an image along with employee data. I am able to do it with different rest calls using jersey. But I want to achieve in one rest call. I provide below the structure. Please help me how to do in this regard.
@POST
@Path("/upload2")
@Consumes({MediaType.MULTIPART_FORM_DATA,MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response uploadFileWithData(
@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition contentDispositionHeader,
Employee emp) {
//..... business login
}
Whenever I am trying to do, I get error in Chrome postman. The simple structure of my Employee json is given below.
{
"Name": "John",
"Age": 23,
"Email": "john@gmail.com",
"Adrs": {
"DoorNo": "12-A",
"Street": "Street-11",
"City": "Bangalore",
"Country": "Karnataka"
}
}
However I can do it by making two different call, but I want to achieve in one rest call so that I can receive the file as well as the actual data of the employee.
Request you to help in this regard.
You can't have two
Content-Type
s (well technically that's what we're doing below, but they are separated with each part of the multipart, but the main type is multipart). That's basically what you are expecting with your method. You are expecting mutlipart and json together as the main media type. TheEmployee
data needs to be part of the multipart. So you can add a@FormDataParam("emp")
for theEmployee
.Here's the class I used for testing
First I just tested with the client API to make sure it works
I just created a simple
Employee
class with anid
andname
field for testing. This works perfectly fine. It shows the image, prints the content disposition, and prints theEmployee
object.I'm not too familiar with Postman, so I saved that testing for last :-)
It appears to work fine also, as you can see the response
"Cool Tools"
. But if we look at the printedEmployee
data, we'll see that it's null. Which is weird because with the client API it worked fine.If we look at the Preview window, we'll see the problem
There's no
Content-Type
header for theemp
body part. You can see in the client API I explicitly set itSo I guess this is really only part of a full answer. Like I said, I am not familiar with Postman So I don't know how to set
Content-Type
s for individual body parts. Theimage/png
for the image was automatically set for me for the image part (I guess it was just determined by the file extension). If you can figure this out, then the problem should be solved. Please, if you find out how to do this, post it as an answer.And just for completeness...
Basic configurations:
Dependency:
Client config:
Server config:
UPDATE
So as you can see from the Postman client, some clients are unable to set individual parts' Content-Type, this includes the browser, in regards to it's default capabilities when using
FormData
(js).We can't expect the client to find away around this, so what we can do, is when receiving the data, explicitly set the Content-Type before deserializing. For example
It's a little extra work to get the POJO, but it is a better solution than forcing the client to try and find it's own solution.
Asides
I used file upload example from,
http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/
in my resource class i have below method
in my attachService.java i have below method
in Dao i have
in my HBM mapping is like
This working for all type of files like .PDF,.TXT, .PNG etc.,
Your ApplicationConfig should register the MultiPartFeature.class from the glassfish.jersey.media.. so as to enable file upload
Set the "Content-Type: multipart/form-data" on the client side and that should do the job
I want add a comment on peeskillet but don't have 50 reputation points, hence adding as an answer:
When I tried @peeskillet solution with Jersey client 2.21.1, there was 400 error. It worked when I added following in my client code:
instead of hardcoded MediaType.MULTIPART_FORM_DATA in post request call.