java - 映射为 Rest API Post 请求中的参数

标签 java spring spring-boot spring-mvc postman

我创建了一个带有 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/

相关文章:

java - Spring Boot无法连接MySQL : The server time zone value 'Mitteleuropäische Zeit' is unrecognized

java - 在 JSF 和 Richfaces 中处理过期的 ViewState

java - 扩展 Hibernate 的 ResourceBundleMessageInterpolator

java - <form :input > to map with object attribute in spring 中路径的动态值

java - 是否可以在 Spring 中从非事务方法调用事务方法?

jpa - Flyway Spring Boot Autowired Beans with JPA Dependency

java - 如何在 hibernate 中记录失败的sql?

java - 在 64 位机器上启动 STS 时收到 Java 已启动但返回退出代码=13 错误

java - 安卓 : How to delete after so many characters?

node.js - 通过 Gradle 将文件复制到构建目录