我创建了一个带有 Map<String, Integer>
的 API参数,像这样:
@RequestMapping(value = "upload", method = RequestMethod.POST)
public ResponseEntity<String> handleContactsFileUpload(@RequestParam("file") MultipartFile file,
@RequestParam("name") String name,
@RequestParam("campaignAppItemId") Long campaignAppItemId,
@RequestParam("fileColumnHeaders") Map<String,Integer> fileColumnHeaders) throws Exception {
if (file == null)
return new ResponseEntity<>("No file uploaded", HttpStatus.BAD_REQUEST);
contactService.handleContactsFile(file, name, campaignAppItemId,fileColumnHeaders);
return new ResponseEntity<>("File uploaded successfully", HttpStatus.OK);
}
我正在尝试通过 Postman 调用它:
我通过了 fileColumnHeaders
在 Body->Form Data 中,如屏幕截图所示。
然后我在 Postman 中收到了这样一条消息:
Failed to convert value of type 'java.lang.String' to required type 'java.util.Map'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.util.Map': no matching editors or conversion strategy found.
有人知道为什么会收到这条消息吗? 我们如何在 Rest API 请求中将 map 作为参数传递? 我们如何通过 Postman 传递 map ?
最佳答案
您可以使用 @RequestBody
而不是 @RequestParam
来处理 map 和其他重要的数据类型和对象 - 这样 spring 会将表示您的 map 参数的 JSON 映射到域对象,然后可序列化并可以转换为 java 对象。
关于java - 映射为 Rest API Post 请求中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54573001/