2015-02-23 186 views
1

我试图通过jackson-mapper-asl库的帮助将对象列表转换为json,但作为响应,我得到http 406错误。 jackson-mapper-asl库位于我的课程路径中。以下是我的Spring MVC控制器代码:Spring MVC:jackson-mapper-asl不适用于Spring 4

@RequestMapping(value="/find-sub-categories/{parentId}", method=RequestMethod.GET) 
@ResponseBody public List<ProductCategory> findSubCategories(@PathVariable(value="parentId") ProductCategory productCategory) { 
    logger.info("In ADMIN findSubCategories Contoller AJAX GET"); 

    List<ProductCategory> categories = productCategoryService.findAllChildProductCategoriesByParent(productCategory); 
    return categories; 
} 

我的Ajax请求代码:

$.ajax({ 
      url: url+"/"+parentId, 
      type: 'GET', 
      success: function(result) { 
       var arr = $.parseJSON(result); 
----------------------- 
+3

你使用错误的杰克逊依赖关系检查这里http://stackoverflow.com/questions/26825276/spring-4-restcontroller-json-characteristics-not-acceptable-according-to-the-re – 2015-02-23 14:42:40

+0

你加入?contentType:“应用程序/ json; charset = utf-8“,dataType:”json“,也是ver杰克逊的使用?对Jackson的支持一直专注于2.0以上的春季4 – 2015-02-23 16:24:24

回答

10

随着spring 4,我们需要使用以下依赖性:

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

jackson-mapper-asl用于与spring 3.x 。感谢@Master Slave的有效评论。点击这里查看更多信息:Spring 4 RestController JSON: characteristics not acceptable according to the request "accept" headers

+0

如果我添加上面的依赖关系,我的tomcat服务器8就停止了。会有什么问题? – 2017-07-31 13:59:19

0

我想你在有效路径变量参数类型前使用:

@RequestMapping(value="/find-sub-categories/{parentId}", method=RequestMethod.GET) 
@ResponseBody public List<ProductCategory> [email protected](value="parentId") ProductCategory productCategory) 

,而不是它,使用下面的方法头它可能工作

@ResponseBody public List<ProductCategory> findSubCategories(@PathVariable Integer parentId) 
+0

我使用'@ PathVariable'的方式是Spring-Data功能。这个问题只与我使用的错误依赖关系有关。我已经发布了答案。 – 2015-02-24 05:20:14