【问题标题】:Using Map as a @RequestBody in Spring Boot Rest API is not working在 Spring Boot Rest API 中使用 Map 作为 @RequestBody 不起作用
【发布时间】:2026-02-09 18:50:01
【问题描述】:

我想从客户端检索一个自定义 json 对象,我正在使用地图读取该对象的帖子正文。但是当我尝试访问 API 时,我得到了java.lang.NoSuchMethodException: java.util.Map.<init>()。我很确定它应该可以工作,因为我在以前的项目中编写了类似的 API。我通过再次运行该项目再次进行了交叉检查。有人可以帮助我在这里缺少什么。如果有任何解决方案来读取自定义 JSON 对象,也欢迎。

我添加了以下依赖

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

API 代码:

    @PostMapping("/data")
    public ResponseEntity<Map> postInfo(@RequestBody Map input) {
        
        System.out.println("Input data: "+input);
        
        Map response = new HashMap<>();
        response.put("message", "input received");
        
        return new ResponseEntity<Map>(response, HttpStatus.OK);
    }

邮递员:

控制台日志:

java.lang.NoSuchMethodException: java.util.Map.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_261]
    at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_261]
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:216) ~[spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:85) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:139) ~[spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167) ~[spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134) ~[spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) ~[tomcat-embed-core-9.0.41.jar:4.0.FR]

另外,我将 Map 更改为 HashMap,如下所示。这次没有抛出任何错误,但输入映射为空,即使我正在传递输入对象。我无法获取帖子正文。

    @PostMapping("/data")
    public ResponseEntity<Map> postInfo(@RequestBody HashMap input) {
        
        System.out.println("Input data: "+input);
        
        Map response = new HashMap<>();
        response.put("message", "input received");
        
        return new ResponseEntity<Map>(response, HttpStatus.OK);
    }

邮递员:

控制台日志:

Input data: {}

【问题讨论】:

    标签: spring-boot rest jackson hashmap jackson-databind


    【解决方案1】:

    这可能听起来很傻,但您可以检查 RequestBody 是否真的被导入。它应该是这样的:

    import org.springframework.web.bind.annotation.RequestBody;
    

    我遇到了同样的问题,添加这个导入就可以了。

    【讨论】:

    • 我也发现了这个问题。但没有更新答案
    【解决方案2】:

    我创建了版本为“2.2.2.RELEASE”的 Spring Boot Web 项目以获取相同版本的 Spring Web jar。我可以毫无问题地使用 map 读取 json。

    我观察到一件事是在我本地的 HandlerMethodArgumentResolver 被选为“org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor@602cf534”

    但根据您的异常,它被选为“org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:139) "

    邮递员截图:

    日食截图:

    【讨论】:

      【解决方案3】:

      使用@requestParam

       @PostMapping("/data")
      public ResponseEntity<Map> postInfo(@requestParam HashMap input) {
          
          System.out.println("Input data: "+input);
          
          Map response = new HashMap<>();
          response.put("message", "input received");
          
          return new ResponseEntity<Map>(response, HttpStatus.OK);
      }
      

      【讨论】:

        【解决方案4】:

        您需要根据您的情况指定 Map 对象的类型,例如 Map

        @PostMapping("/data")
        public ResponseEntity<Map> postInfo(@RequestBody Map<String, String> input) {
            
            System.out.println("Input data: "+input);
            
            Map response = new HashMap<>();
            response.put("message", "input received");
            
            return new ResponseEntity<Map>(response, HttpStatus.OK);
        }
        

        【讨论】:

        • 没有类型也可以。我也尝试过类型。它不工作