作者:专业长膘 | 来源:互联网 | 2023-10-17 14:01
我将SpringMVC升级到Spring3.2.5.我的一些剩余调用即使存在,也会返回无法识别的字段异常.这是错误.Resolvingexceptionfrom
我将Spring MVC升级到Spring 3.2.5.我的一些剩余调用即使存在,也会返回无法识别的字段异常.这是错误.
Resolving exception from handler [public com.app.common.web.datatables.DataTablesAjaxResponse 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 dataProp;
@JsonProperty("sSearch")
private String searchQuery;
@JsonProperty("asSearch")
private List columnSearches;
@JsonProperty("bRegex")
private boolean hasRegex;
@JsonProperty("abRegex")
private List regexColumns;
@JsonProperty("abSearchable")
private List searchColumns;
@JsonProperty("iSortingCols")
private int sortingCols;
@JsonProperty("aiSortCol")
private List sortedColumns;
@JsonProperty("asSortDir")
private List sortDirections;
@JsonProperty("abSortable")
private List sortableColumns;
@JsonProperty("aoFilters")
private Map filters;
//Setters and getters
这是控制器.那就是调用API
@RequestMapping(value = AJAX_SEARCH_MED, method = RequestMethod.POST)
@ResponseBody
DataTablesAjaxResponse ajaxSearchActiveMedication(
@PathVariable(PATH_PIN) String pin,
@RequestBody DataTablesAjaxRequest request);
我正在使用Spring MVC 3.2.5和org.codehaus.jackson-jackson-mapper-asl版本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":""
}
}
解决方法:
由于已将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一起使用