简体   繁体   中英

ReST with Spring: Sending multi-part form data along with other input data

I have referred to this question, but still the answer is not clear.

Here's the scenarios where I am facing an issue:

  1. User can upload documents
  2. Along with the documents, the user also sends some other information like name, phone number, etc.
  3. The requests are being sent in JSON format from the front end.
  4. With @Consumes(MediaType.MULTIPART_FORM_DATA) , we can only accept the file related data and it works fine. But when the request contains other details like name or phone number, the server throws "Unsupported Media Type" exception.

How can we handle this scenario? We are using Jersey Rest and the front end sends AJAX requests.

Finally figured it out. We can access the additional attributes using @FormDataParam itself. So, the method signature will look like -

public void createRecord(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("firstName") String firstName, @FormDataParam("lastName") String lastName)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM