0

I have to write a REST API whcih consumes multipart/form-data. When I write the method like below , it works fine

@RequestMapping(value="/addSftp", method=RequestMethod.POST)
public HashMap<String, Object> welcome( HttpServletRequest request,@RequestParam(value="selectedFile", required=false) MultipartFile selectedFile,
        @RequestParam(value="sftp_name") String sftp_name,
        @RequestParam(value="ip_address")String ip_address,
        @RequestParam(value="port_number")String port_number,
        @RequestParam(value="userName")String userName,
        @RequestParam(value="password")String password,
        @RequestParam(value="customRadioInline1")String customRadioInline1) {
    HashMap map = new HashMap<String, Object>();
    System.out.println(sftp_name);
    System.out.println(ip_address);
    System.out.println(port_number);
    System.out.println(userName);
    System.out.println(password);
    System.out.println(customRadioInline1);
    System.out.println(selectedFile);
    map.put("stat", "success");
     return map;
              }

I want to map the request to a bean. Below is my bean

public class SftpBean {

    private String sftp_name;
    private String ip_address; 
    private String port_number;
    private String userName;
    private String password;
    private String customRadioInline1;
    private MultipartFile selectedFile;

//getters and setters here

}

But when I write the method like below , I am getting exception

public HashMap<String, Object> welcome( @RequestBody SftpBean sftpBean) {
    HashMap map = new HashMap<String, Object>();
    System.out.println(sftpBean.getIpAddress());
    //similar statements for priniting other parameters
    map.put("stat", "success");
     return map;
}

Here is my exception

Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: 
    Content type 'multipart/form-data;boundary=---------------------------246741595337214313524058968681;charset=UTF-8' not supported]

Here is my request payload

-----------------------------317042354532732980343175029806
Content-Disposition: form-data; name="sftp_name"

ABC
-----------------------------317042354532732980343175029806
Content-Disposition: form-data; name="ip_address"

123
-----------------------------317042354532732980343175029806
Content-Disposition: form-data; name="port_number"

456
-----------------------------317042354532732980343175029806
Content-Disposition: form-data; name="userName"

demo
-----------------------------317042354532732980343175029806
Content-Disposition: form-data; name="password"

demo123
-----------------------------317042354532732980343175029806
Content-Disposition: form-data; name="customRadioInline1"

pwd
-----------------------------317042354532732980343175029806
Content-Disposition: form-data; name="selectedFile"; filename="details.txt"
Content-Type: text/plain

Is there a way to directly map the request to my SftpBean?

  • Does this answer your question? [Spring MVC Multipart Request with JSON](https://stackoverflow.com/questions/21329426/spring-mvc-multipart-request-with-json) – Benjamin Maurer Jun 26 '20 at 14:10
  • @KavithaKarunakaran I am just printing out the variable names in logic. However I will add it – srinivas chaitanya Jun 26 '20 at 14:11
  • @srinivaschaitanya - The error says that your content type is not supported at the server end. Also, from the sample code it is not evident whether you have used `@Consumes` to specify the content type accepted by the welcome method in your RestController – Kavitha Karunakaran Jun 26 '20 at 14:18
  • @KavithaKarunakaran tried using consumes=MediaType.APPLICATION_JSON_VALUE But I got the same error – srinivas chaitanya Jun 26 '20 at 14:43
  • Ok. You need to do the following two things - a). change the consumes to `MediaType.APPLICATION_JSON` instead of ``MediaType.APPLICATION_JSON_VALUE``. b). Change the payload from multipart form to json format. – Kavitha Karunakaran Jun 26 '20 at 15:05

2 Answers2

1

You can create a RequestPart for body you are sending along with MultipartFile.

Here is how i have done it:

@PostMapping("/hello/upload")
public SftpBean upload(@RequestParam("file") MultipartFile file,
                      @RequestPart("body") SftpBean sftpBean) {
    return sftpBean;
}

Pojo remains the same.

Request in postman: enter image description here

silentsudo
  • 6,730
  • 6
  • 39
  • 81
0

You need to add @Consumes or consumes= inside @RequestMapping to specify the content type acceptable by the server end. If you are sending multipart form data, you can use MediaType.MULTIPART_FORM_DATA if you are sending the request as multipart form. However, if you are encapsulating that into an object and sending it as json, you need to use MediaType.APPLICATION_JSON.

Also ensure that the right format is sent from the client side which consumes the RestController. You need to change the payload from multipart form to json format at client end while creating the REST request. The modified method :

public HashMap<String, Object> welcome( @RequestBody SftpBean sftpBean)

expects only a json object SftpBean which is not at all found in your payload.

Kavitha Karunakaran
  • 1,340
  • 1
  • 17
  • 32