在 Spring Boot Rest API 中使用 Map 作为 @RequestBody 不起作用

问题描述 投票:0回答:5

我想从客户端检索一个自定义 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
5个回答
2
投票

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

1
投票

这可能听起来很愚蠢,但您可以检查 RequestBody 是否确实被导入。它应该看起来像这样:

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

我面临着同样的问题,添加这个导入就解决了问题。


0
投票

在绑定请求正文时,如果不指定类型(通配符),则无法使用通用集合/容器。

例如:

Map<String, Object>, List<Object>, Vector<Object>

此外,如果您将键或值绑定到自定义对象类型(可能是第三方类型而不是您自己的类型),Spring 需要能够正确地序列化/反序列化它。

这意味着您可能需要创建一个自定义序列化器/解串器,或者如果第三方提供了一个,则需要正确设置它。


-1
投票

我创建了版本“2.2.2.RELEASE”的 Spring Boot 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) ”

邮递员截图:

postman-screenshot

eclipse-屏幕截图:

eclipse-screenshot


-2
投票

使用

@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);
}
© www.soinside.com 2019 - 2024. All rights reserved.