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

esbemjvw  于 6个月前  发布在  其他
关注(0)|答案(5)|浏览(78)

我想从客户端检索一个自定义JSON对象,我正在使用Map阅读帖子正文。但是,当我试图击中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 :x1c 0d1x
控制台日志:

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,如下所示。这次没有抛出任何错误,但输入map是空的,即使我传递了input Object。我无法获得post body。

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

控制台日志:

Input data: {}

gxwragnw

gxwragnw1#

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

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

字符串
我也遇到了同样的问题,添加这个导入就解决了这个问题。

5n0oy7gb

5n0oy7gb2#

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

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

字符串

yhived7q

yhived7q3#

在绑定请求体时,如果不指定它们的类型(type,简写为CSTR),就不能使用泛型集合/容器。
举例来说,您可以:

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

字符串
此外,如果您将键或值绑定到自定义对象类型(可能是第三方类型,而不是您自己的类型),Spring需要能够正确地序列化/重新序列化它。
这意味着您可能需要创建一个自定义的序列化器/反序列化器,或者如果第三方提供了一个,则需要正确地设置它。

kr98yfug

kr98yfug4#

我创建了Sping Boot Web项目,版本为“2.2.2.RELEASE”,以获得相同版本的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)“
postman-screenshot:
x1c 0d1x的数据
eclipse截图:


rbl8hiat

rbl8hiat5#

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

字符串

相关问题