spring-boot - 在 Spring Boot Rest API 中将 Map 用作 @RequestBody 不起作用

标签 spring-boot rest jackson hashmap jackson-databind

我想从我正在使用 map 读取帖子正文的客户端检索自定义 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);
    }

postman : enter image description here

控制台日志:

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);
    }

postman : enter image description here

控制台日志:

Input data: {}

最佳答案

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

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

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

关于spring-boot - 在 Spring Boot Rest API 中将 Map 用作 @RequestBody 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66025938/

相关文章:

java - 通过使用<img th:src = “@{/images/anabada.jpeg}”无法加载thyemleaf中的图像

rest - 创建工作正常,但 neo4j post params 中的 MERGE 有错误

http - 细粒度 rest HTTP 动词浏览器支持

java - 是否能够在构建过程中将 REST 端点标记为忽略?

java - 如何使用对象字段本身给定的格式解析输入对象日期字段

java - 在 Spring Boot 上发布 DTO 对象后的模型属性

java - Spring Boot 应用程序的 POM

java - "mvn spring-boot:run"与 "java jar target/xxx.jar"

使用 JSON 和 JSP 的 Spring MVC Controller

java - Gson 有类似 WRITE_NULL_MAP_VALUES (Jackson) 的东西吗?