HttpMessageNotWritableException:No converter found for return value of type: class java.util.HashMap

请求springmvc中的接口时服务端出现24-Sep-2020 20:08:51.902 警告 [http-nio-8080-exec-18] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logException Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.HashMap],相关代码如下:

@ResponseBody
@GetMapping("/ajaxAddItem/{id}")
public Map<String, Object> ajaxAddItem(@PathVariable("id") Integer bookId, HttpServletRequest request) {
    Book book = bookService.queryBookById(bookId);
    CartImpl cart = (CartImpl)request.getSession().getAttribute("cart");
    if (cart == null) {
        cart = new CartImpl();
        request.getSession().setAttribute("cart", cart);
    }
    cart.addItem(new CartItem(book.getId(), book.getName(), 1, book.getPrice()));
    request.getSession().setAttribute("lastAddBook", book.getName());

    Map<String, Object> result = new HashMap<>();
    result.put("lastAddBook", book.getName());
    result.put("cartCount", cart.getTotalCount());
    return result;
}

出现此问题的原因是使用了jackson中的功能(@ResponseBody)但是缺少了部分jar包,导入以下maven依赖即可(全都要):

<!--<dependency>
 <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson.version}</version>
</dependency>-->
<!--依赖,所以包含-->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson.version}</version>
</dependency>
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:设计师小姐姐 返回首页