gpt4 book ai didi

java - Spring MVC 3.2.5 无法识别的字段 jackson

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:13:09 24 4
gpt4 key购买 nike

我将 spring mvc 升级到 spring 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

这是 Controller 。那就是调用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 版本 1.9.9 使用的消息转换器是 MappingJackson2HttpMessageConterter。当它在 3.1 版时一切正常,但是当我升级到 Spring 3.2.5 时会显示这些错误。

更新

这是请求 header

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 代替

关于java - Spring MVC 3.2.5 无法识别的字段 jackson ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24583845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com