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

Spring boot 在Spring Boot Rest API中将Map用作@RequestBody不起作用,spring-boot,rest,jackson,hashmap,jackson-databind,Spring Boot,Rest,Jackson,Hashmap,Jackson Databind,我想从客户机中检索一个自定义json对象,我正在使用映射为其读取文章正文。但是当我尝试点击API时,我得到了java.lang.NoSuchMethodException:java.util.Map.()。我非常确信它应该可以工作,因为我在以前的项目中编写了类似的API。我通过再次运行该项目再次进行了交叉检查。有人能帮我一下我在这里错过了什么吗。如果有任何解决方案可以读取自定义JSON对象,这也是受欢迎的 我添加了以下依赖项 <dependency>

我想从客户机中检索一个自定义json对象,我正在使用映射为其读取文章正文。但是当我尝试点击API时,我得到了
java.lang.NoSuchMethodException:java.util.Map.(
)。我非常确信它应该可以工作,因为我在以前的项目中编写了类似的API。我通过再次运行该项目再次进行了交叉检查。有人能帮我一下我在这里错过了什么吗。如果有任何解决方案可以读取自定义JSON对象,这也是受欢迎的

我添加了以下依赖项

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

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

@PostMapping(“/data”)
公共响应postInfo(@RequestBody映射输入){
System.out.println(“输入数据:”+输入);
Map response=newhashmap();
响应。输入(“消息”,“收到输入”);
返回新的ResponseEntity(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);
}
@PostMapping(“/data”)
公共响应postInfo(@requestParam HashMap输入){
System.out.println(“输入数据:”+输入);
Map response=newhashmap();
响应。输入(“消息”,“收到输入”);
返回新的ResponseEntity(response,HttpStatus.OK);
}

我创建了版本为“2.2.2.RELEASE”的SpringBootWeb项目,以获得相同版本的SpringWebJAR。我能够使用map阅读json,而不存在任何问题

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

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

邮递员截图:

eclipse屏幕截图:


如果没有类型,它也可以工作。我也试过打字。它不起作用
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]
    @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: {}
@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);
}
 @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);
}