带有混合多部分请求的@RequestPart,Spring MVC 3.2
作者:互联网
我正在开发基于Spring 3.2的RESTful服务.我正面临一个控制器处理混合多部分HTTP请求的问题,第二部分使用XML或JSON格式化数据,第二部分使用图像文件.
我正在使用@RequestPart annotation 来接收请求
@RequestMapping(value = "/User/Image", method = RequestMethod.POST, consumes = {"multipart/mixed"},produces="applcation/json")
public
ResponseEntity<List<Map<String, String>>> createUser(
@RequestPart("file") MultipartFile file, @RequestPart(required=false) User user) {
System.out.println("file" + file);
System.out.println("user " + user);
System.out.println("received file with original filename: "
+ file.getOriginalFilename());
// List<MultipartFile> files = uploadForm.getFiles();
List<Map<String, String>> response = new ArrayList<Map<String, String>>();
Map<String, String> responseMap = new HashMap<String, String>();
List<String> fileNames = new ArrayList<String>();
if (null != file) {
// for (MultipartFile multipartFile : files) {
String fileName = file.getOriginalFilename();
fileNames.add(fileName);
try {
file.transferTo(new File("C:/" + file.getOriginalFilename()));
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
responseMap.put("displayText", file.getOriginalFilename());
responseMap.put("fileSize", "" + file.getSize());
response.add(responseMap);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Accept", "application/json");
return new ResponseEntity<List<Map<String, String>>>(response,
httpHeaders, HttpStatus.OK);
}
User.java会像这样 –
@XmlRootElement(name = "User")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private int userId;
private String name;
private String email;
private String company;
private String gender;
//getter setter of the data members
}
根据我的理解,使用@RequestPart注释我希望根据其Content-Type评估XML多部分,最后将其解组到我的User类中(我使用的是Jaxb2,marshaller / unmarhaller已正确配置在当我将XML数据作为主体传递并使用@RequestBody注释时,应用程序上下文和过程适用于所有其他控制器方法.
但实际发生的是,尽管文件被正确地找到并解析为MultipartFile,但“用户”部分从未被看到,并且请求总是失败,与控制器方法签名不匹配.
我用几种客户端类型重现了这个问题,我相信多部分请求的格式是可以的.
请帮我解决这个问题,也许会有一些解决方法来接收混合/多部分请求.
谢谢并恭祝安康,
Raghvendra
解决方法:
不确定你是否已经修复了你的问题,但是我也有一个类似的问题,当我将JSON对象与@RequestPart和MultipartFile混合在一起时,我的控制器没有把它拿起来.
您的通话方法签名看起来是正确的:
public ResponseEntity<List<Map<String, String>>> createUser(
@RequestPart("file") MultipartFile file, @RequestPart(required=false) User user) {
// ... CODE ...
}
但请确保您的请求看起来像这样:
POST /createUser
Content-Type: multipart/mixed; boundary=B0EC8D07-EBF1-4EA7-966C-E492A9F2C36E
--B0EC8D07-EBF1-4EA7-966C-E492A9F2C36E
Content-Disposition: form-data; name="user";
Content-Type: application/xml; charset=UTF-8
<user><!-- your user xml --></user>
--B0EC8D07-EBF1-4EA7-966C-E492A9F2C36E
Content-Disposition: form-data; name="file"; filename="A551A700-46D4-470A-86E7-52AD2B445847.dat"
Content-Type: application/octet-stream
/// FILE DATA
--B0EC8D07-EBF1-4EA7-966C-E492A9F2C36E--
标签:spring,rest,multipartform-data 来源: https://codeday.me/bug/20190930/1836127.html