我正在尝试使用 Jackson 序列化一个包含两个字符串和字符串到字符串的映射的类。这是我正在尝试序列化的 json。我想知道是否存在问题,因为我正在尝试序列化空数组。
{
"filters": {
"test": [
"hi"
],
"groups": [],
"groupsOT": [],
"chains": [],
"chainsOT": [],
"locations": [],
"locationsOT": [],
"reports": [],
"reportsOT": []
},
"fromDate": "09.03.2015",
"toDate": "16.03.2015"
}
这是用于尝试序列化它的类。
public class FilterRequest{
public String getToDate() {
return toDate;
}
public void setToDate(String toDate) {
this.toDate = toDate;
}
public String getFromDate() {
return fromDate;
}
public void setFromDate(String fromDate) {
this.fromDate = fromDate;
}
public Map<String, String[]> getFilters() {
return filters;
}
public void setFilters(Map<String, String[]> filters) {
this.filters = filters;
}
private String toDate;
private String fromDate;
private Map<String,String[]> filters;
public FilterRequest(){
filters = new HashMap<String,String[]>();
}
}
失败的代码很简单
ObjectMapper mapper = new ObjectMapper();
FilterRequest requestParams = mapper.readValue(requestBody, FilterRequest.class);
我收到的错误是
No suitable constructor found for type [simple type, class com.aramburu.overall.web.controller.FilterController$FilterRequest]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: {"filters":{"test":["hi"],"groups":[],"groupsOT":[],"chains":[],"chainsOT":[],"locations":[],"locationsOT":[],"reports":[],"reportsOT":[]},"fromDate":"09.03.2015","toDate":"16.03.2015"}; line: 1, column: 2]
最佳答案
输出:
没有找到适合类型[简单类型,类 com.aramburu.overall.web.controller.FilterController$FilterRequest
的合适构造函数,意味着 FilterRequest
是一个内部类。
将 FilterRequest
类设为静态(或者更好 - 将其移出 FilterController
)。
否则 Jackson 无法实例化它(它需要父类的外部实例才能构造内部类的实例)。
关于java - Jackson 无法使用 ObjectMapper 映射类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29078089/