Java 将多部分表单数据映射到对象

Java 将多部分表单数据映射到对象,java,spring-boot,Java,Spring Boot,我必须编写一个RESTAPI,它使用多部分/表单数据。当我写下面这样的方法时,它工作得很好 @RequestMapping(value="/addSftp", method=RequestMethod.POST) public HashMap<String, Object> welcome( HttpServletRequest request,@RequestParam(value="selectedFile", required=false

我必须编写一个RESTAPI,它使用
多部分/表单数据
。当我写下面这样的方法时,它工作得很好

@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;
              }
但当我编写下面这样的方法时,我得到了一个异常

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;
}
Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: 
    Content type 'multipart/form-data;boundary=---------------------------246741595337214313524058968681;charset=UTF-8' not supported]
这是我的请求

-----------------------------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

是否有一种方法可以直接将请求映射到我的
SftpBean

您需要在
@RequestMapping
中添加
@Consumes
以指定服务器端可接受的内容类型。如果您发送的是多部分表单数据,如果您以多部分表单的形式发送请求,则可以使用
MediaType.multipart\u form\u data
。但是,如果要将其封装到对象中并作为json发送,则需要使用
MediaType.APPLICATION\u json

还要确保从使用
RestController
的客户端发送正确的格式。在创建REST请求时,需要在客户端将负载从多部分格式更改为json格式。修改后的方法:

public HashMap<String, Object> welcome( @RequestBody SftpBean sftpBean)
公共HashMap欢迎(@RequestBody-SftpBean-SftpBean)

只需要一个json对象
SftpBean
,该对象在您的负载中根本找不到。

您可以为随
多部分文件一起发送的正文创建一个
RequestPart

我是这样做的:

@PostMapping(“/hello/upload”)
公共SFTPean上载(@RequestParam(“文件”)多部分文件,
@请求部分(“正文”)SftpBean(SftpBean){
返回SFTPean;
}
Pojo仍然保持不变

邮递员要求:

这是否回答了您的问题@KavithaKarunakaran我只是用逻辑打印变量名。不过,我要补充一点it@srinivaschaitanya-错误表示服务器端不支持您的内容类型。此外,从示例代码看,您是否使用了
@Consumes
来指定您的应用程序中欢迎方法接受的内容类型并不明显RestController@KavithaKarunakaran尝试使用consumes=MediaType.APPLICATION_JSON_值,但得到了相同的错误OK。你需要做以下两件事-a)。将消费更改为
MediaType.APPLICATION\u JSON
,而不是
MediaType.APPLICATION\u JSON\u值
。b) 。将有效负载从多部分格式更改为json格式。
public HashMap<String, Object> welcome( @RequestBody SftpBean sftpBean)