在 Spring Boot Rest API 中将 Map 用作 @RequestBody 无效

Using Map as a @RequestBody in Spring Boot Rest API is not working

我想从客户端检索自定义 json 对象,我正在使用地图为其读取 post 正文。但是当我尝试点击 API 时,我得到了 java.lang.NoSuchMethodException: java.util.Map.<init>()。我很确定它应该可以工作,因为我在以前的项目中写过类似的 APIs。我再次通过 运行 那个项目进行了交叉检查。有人可以帮助我在这里缺少什么。如果有任何变通解决方案来读取自定义 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,如下所示。这次没有抛出任何错误,但输入映射为空,即使我正在传递输入对象。我无法获得 post 正文。

    @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: {}

您需要根据您的情况指定 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);
}

使用@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);
}

我创建了 spring 版本为 '2.2.2.RELEASE' 的引导 Web 项目以获得相同版本 spring Web jar。我可以使用地图阅读 json,没有任何问题。

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

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

邮递员截图:

eclipse-截图:

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

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

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