未找到类型返回值的转换器:类 java.util.HashMap

Posted

技术标签:

【中文标题】未找到类型返回值的转换器:类 java.util.HashMap【英文标题】:No converter found for return value of type: class java.util.HashMap 【发布时间】:2019-11-04 01:00:36 【问题描述】:

我创建了一个使用 RestController 注释返回 ResponseEntity 的方法,如下所示。

但是,调用此 URI 会导致错误。我添加了所有与 Json 相关的依赖项,但我不知道为什么。

@RestController
@RequestMapping(value="/api")
public class ApiController 

    @Resource(name = "BoardService")
    private BoardService boardSvc;

    @RequestMapping(value="/board/list")
    public ResponseEntity<Map<String, List<?>>> getBoardList() throws Exception
        Map<String, List<?>> resultMap = new HashMap<>();
        List<?> boardList = null;

        boardList = boardSvc.getBoardList();

        resultMap.put("Success!", boardList);

        return new ResponseEntity<>(resultMap, HttpStatus.OK);
       

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.9</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.9</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.9</version>
</dependency>
java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.HashMap
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:187) ~[spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:208) ~[spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81) ~[spring-web-4.3.16.RELEASE.jar:4.3.16.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:113) ~[spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) [spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) [spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) [spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) [servlet-api.jar:?]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) [spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
...

【问题讨论】:

检查***.com/questions/37841373/… @Abi 我已经看过了,但对我没有帮助。 我正在尝试重新创建这个,Boardservice 是什么? 【参考方案1】:

就用springboot mvc更简单的解决方案:

pom:

 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <start-class>Application</start-class>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>


<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

控制器:

@RestController
public class MyController 
    @RequestMapping(value="/getMapResult")
    public @ResponseBody
    Map<String,List<?>> getMapResult() 
        Map<String,List<?>> result = new HashMap<>();
        result.put("a", Arrays.asList("a1","a2"));
        result.put("b",Arrays.asList(1,2,3));
        return result;
    

测试:

curl http://localhost:8080/getMapResult
"a":["a1","a2"],"b":[1,2,3]

【讨论】:

以上是关于未找到类型返回值的转换器:类 java.util.HashMap的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 ResponseEntity 返回 JSONObject 而不是 HashMap? (未找到类型返回值的转换器:类 org.json.JSONObject)

未找到 Java 类 java.util.ArrayList、Java 类型类 java.util.ArrayList 和 MIME 媒体类型 application/json 的消息正文编写器

org.springframework.http.converter.HttpMessageNotWritableException 没有找到类型的返回值的转换器:类

DateDateFormatCalendarSystemMath类总结

java中时间类型转换

枚举类集对于枚举的支持