Spring MVC 3.2.5无法识别的领域杰克逊

时间:2014-07-05 06:54:11

标签: java json spring spring-mvc jackson

我将我的弹簧mvc升级到弹簧3.2.5。我的一些休息调用即使存在,也会返回一个无法识别的字段异常。这是错误。

Resolving exception from handler [public com.app.common.web.datatables.DataTablesAjaxResponse<com.app.cms.consultation.dto.ActiveMedicationView> com.app.cms.consultation.controller.impl.ActiveMedicationControllerImpl.ajaxSearchActiveMedication(java.lang.String,com.app.common.web.datatables.DataTablesAjaxRequest)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "sEcho" (class com.app.common.web.datatables.DataTablesAjaxRequest), not marked as ignorable (16 known properties: "displayStart", "sortedColumns", "searchQuery", "echo", "sortableColumns", "displayLength", "columnSearches", "dataProp", "hasRegex", "sortDirections", "searchColumns", "regexColumns" [truncated]])
 at [Source: org.eclipse.jetty.server.HttpInput@4dc300ae; line: 1, column: 11] (through reference chain: com.app.common.web.datatables.DataTablesAjaxRequest["sEcho"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "sEcho" (class com.app.common.web.datatables.DataTablesAjaxRequest), not marked as ignorable (16 known properties: "displayStart", "sortedColumns", "searchQuery", "echo", "sortableColumns", "displayLength", "columnSearches", "dataProp", "hasRegex", "sortDirections", "searchColumns", "regexColumns" [truncated]])
 at [Source: org.eclipse.jetty.server.HttpInput@4dc300ae; line: 1, column: 11] (through reference chain: com.app.common.web.datatables.DataTablesAjaxRequest["sEcho"])

这是班级:

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public class DataTablesAjaxRequest implements Serializable {

    private static final long serialVersionUID = 1L;

    @JsonProperty("sEcho")
    private String echo;

    @JsonProperty("iColumns")
    private int numColumns;

    @JsonProperty("sColumns")
    private String columns;

    @JsonProperty("iDisplayStart")
    private long displayStart;

    @JsonProperty("iDisplayLength")
    private int displayLength;

    @JsonProperty("amDataProp")
    private List<String> dataProp;

    @JsonProperty("sSearch")
    private String searchQuery;

    @JsonProperty("asSearch")
    private List<String> columnSearches;

    @JsonProperty("bRegex")
    private boolean hasRegex;

    @JsonProperty("abRegex")
    private List<Boolean> regexColumns;

    @JsonProperty("abSearchable")
    private List<Boolean> searchColumns;

    @JsonProperty("iSortingCols")
    private int sortingCols;

    @JsonProperty("aiSortCol")
    private List<Integer> sortedColumns;

    @JsonProperty("asSortDir")
    private List<String> sortDirections;

    @JsonProperty("abSortable")
    private List<Boolean> sortableColumns;

    @JsonProperty("aoFilters")
    private Map<String, String> filters;
    //Setters and getters

这是控制器。那是在调用API

  @RequestMapping(value = AJAX_SEARCH_MED, method = RequestMethod.POST)
    @ResponseBody
    DataTablesAjaxResponse<ActiveMedicationView> ajaxSearchActiveMedication(
            @PathVariable(PATH_PIN) String pin,
            @RequestBody DataTablesAjaxRequest request);

我正在使用Spring MVC 3.2.5和org.codehaus.jackson - jackson-mapper-asl version 1.9.9正在使用的消息转换器是MappingJackson2HttpMessageConterter。当它在版本3.1上时,一切都正常工作,但当我向Spring 3.2.5发布时,会显示这些错误。

更新

这是请求标题

Host: localhost:8081
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/json;charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:8081/cms/patient/2012010000000019/doctor-notes.html
Content-Length: 304
Cookie: JSESSIONID=1gyv2us4wq5fid9v26i59lz1p; __zlcid=
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

这是请求正文

   {
   "sEcho":1,
   "iColumns":7,
   "sColumns":"",
   "iDisplayStart":0,
   "iDisplayLength":10,
   "amDataProp":[
      "prescriptionDtlId",
      "generic",
      "brand",
      "preparation",
      "startDate",
      "endDate",
      "sig"
   ],
   "aiSortCol":[
      0
   ],
   "asSortDir":[
      "asc"
   ],
   "iSortingCols":1,
   "abSortable":[
      false,
      true,
      true,
      false,
      true,
      true,
      false
   ],
   "aoFilters":{
      "generic":""
   }
}

1 个答案:

答案 0 :(得分:4)

由于您已将Spring MVC配置为使用MappingJackson2HttpMessageConverter,因此您需要使用Jackson 2.x注释(com.fasterxml.*)而不是Jackson 1.x注释。

你可以看到Spring MVC正在使用Jackson 2.x,但是你得到的例外

(嵌套异常 com.fasterxml .jackson.databind.exc.UnrecognizedPropertyException)

如果您想坚持使用Jackson 1.x(您可能不应该这样做),则需要配置Spring MVC以与MappingJacksonHttpMessageConverter配合使用