Reputation: 587
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
?
Upvotes: 0
Views: 5979
Reputation: 1400
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.
Upvotes: 0
Reputation: 6973
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.
Upvotes: 1