Home How to create REST endpoint service consume multipart using Spring 4.x?
Reply: 2

How to create REST endpoint service consume multipart using Spring 4.x?

user1541472
1#
user1541472 Published in 2017-12-07 03:04:28Z

I am trying to create a REST service that consumes the "multipart/form-data" with text and attachment objects. For files i can use "org.springframework.web.multipart.MultipartFile", but how do i specify the service method for Text objects?

Below is the expected client call to service.

Kalaiselvan A
2#
Kalaiselvan A Reply to 2017-12-07 03:23:34Z

create Rest endpoint by using this way

  public void sendEmail(@RequestParam String to,@RequestParam String from,@RequestParam String cc,@RequestParam String body, @RequestParam(required = false)MultipartFile[] file){
  }

or

create pojo class for strings

public class Email{
  private String from;
  private String to;
  private String cc;
  private String body;
 // getters and setters
}
 //email should be json string
 public void sendEmail(@RequestParam String email,@RequestParam(required = false) MultipartFile[] file){
   ObjectMapper mapper=new ObjectMapper();
   Email email_pojo=mapper.readValue(email,Email.class);
}
vipin cp
3#
vipin cp Reply to 2017-12-07 04:35:08Z

Just create the controller as specified below:

@RequestMapping(value = "/uploadFiles", headers = ("content-type=multipart/*"), method = RequestMethod.POST)
    public AppDTO uploadFile(UploadFiles uploadFiles, HttpServletRequest request) {

Now create a pojo class for what ever things you need to submit along with a multipartfile member where the file will bind to. refer below code for pojo

class UploadFiles
{
private String cc;
private String to;
private String from;

private MultipartFile attachment;

//create getters and setters

}

MultipartFile here used is provided by spring

import org.springframework.web.multipart.MultipartFile;

If you have more than one attachment you can use MultipartFile[] in POJO

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.31006 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO