I try from my rest client to send files to my web service but i keep getting 415 when i'm sure of the media type i'm sending.
I'm using spring MVC :
Here code of the rest client :
public Response uploadFile(FormDataMultiPart multipart, ...) {
WebTarget target = client.target(uri);
Response response = target.
path(*path*).
request().
header("Content-Type", "multipart/form-data").
post(Entity.entity(multipart, MediaType.MULTIPART_FORM_DATA), Response.class);
return response;
}
And here how i use my function :
@RequestMapping(value = "*map path*", method = RequestMethod.POST)
public ModelAndView saveAttachedFiles(@RequestParam("file") MultipartFile myFile, Model model, HttpServletRequest request) {
try {
File file = new File( myFile.getOriginalFilename());
myFile.transferTo(file);
FileDataBodyPart filePart = new FileDataBodyPart("file", file);
FormDataContentDisposition contentDisposition = FormDataContentDisposition.name("file").fileName(file.getName()).build();
filePart.setContentDisposition(contentDisposition);
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
formDataMultiPart.bodyPart(filePart);
Response reponse = clientRest.uploadFile(formDataMultiPart, ...);
}
Here request header of my client (from logs)
31 > POST ***
31 > Content-Type: multipart/form-data
31 > Cookie: $Version=1;JSESSIONID=***
31 > Referer: ***
--Boundary_1_523348906_1460533877988
Content-Type: image/jpeg
Content-Disposition: form-data; filename="Penguins.jpg"; name="file"
Here code of the web services
@POST
@Path("/file")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition contentDispositionHeader,
@QueryParam("uuid") String uuid) {
AttachedFileDto attachedFileDto;
try {
** Processing the file **
}
}
And the web service response header
31 < 415
31 < Access-Control-Allow-Credentials: true
31 < Access-Control-Allow-Headers: Content-Type, Accept, X-Requested-With,Cache-Control,Pragma
31 < Access-Control-Allow-Methods: GET,POST,HEAD,OPTIONS,PUT,DELETE
31 < Access-Control-Allow-Origin: http://localhost:3333
31 < Connection: Keep-Alive
31 < Content-Length: 0
31 < Content-Type: application/json
31 < Date: Wed, 13 Apr 2016 07:51:16 GMT
31 < Keep-Alive: timeout=5, max=100
31 < Server: Apache
Do you have any clue ?
Thanks =)
Found the solution to my problem, i was confused between PathParam and QueryParam.
Service was like this :
@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition contentDispositionHeader,
@QueryParam("uuid") String uuid)
And i was putting my uuid parameter in path function :
public Response uploadFile(FormDataMultiPart multipart, ...) {
WebTarget target = client.target(uri);
Response response = target.
path(*path*?uuid=12315465).
request().
header("Content-Type", "multipart/form-data").
post(Entity.entity(multipart, MediaType.MULTIPART_FORM_DATA), Response.class);
return response;
}
Added the queryParam function :
public Response uploadFile(FormDataMultiPart multipart, ...) {
WebTarget target = client.target(uri);
Response response = target.
path(*path*).
queryParam("uuid", uuid).
request().
post(Entity.entity(multipart, multipart.getMediaType()));
return response;
}
And now it works =)