简体   繁体   English

杰克逊无法使用ObjectMapper映射类

[英]Jackson unable to Map Class using ObjectMapper

I am trying to serialize a class which contains two strings and a map of string to strings using Jackson. 我正在尝试使用Jackson序列化包含两个字符串和字符串到字符串的映射的类。 Here is the json i am trying to serialize. 这是我要序列化的json。 I am wondering if there is a problem because I am trying to serialize empty arrays. 我想知道是否有问题,因为我正在尝试序列化空数组。

{
    "filters": {
        "test": [
            "hi"
        ],
        "groups": [],
        "groupsOT": [],
        "chains": [],
        "chainsOT": [],
        "locations": [],
        "locationsOT": [],
        "reports": [],
        "reportsOT": []
    },
    "fromDate": "09.03.2015",
    "toDate": "16.03.2015"
}

Here is the class that is being used to try and serialize this. 这是用于尝试对此序列化的类。

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[]>();
    }


}

The code that is failing is simply 失败的代码很简单

    ObjectMapper mapper = new ObjectMapper();
    FilterRequest requestParams = mapper.readValue(requestBody, FilterRequest.class);

The error I am getting is 我得到的错误是

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]

The output: No suitable constructor found for type [simple type, class com.aramburu.overall.web.controller.FilterController$FilterRequest implies that FilterRequest is an inner-class. 输出: No suitable constructor found for type [simple type, class com.aramburu.overall.web.controller.FilterController$FilterRequest这表明FilterRequest是内部类。

Make the FilterRequest class static (or - better still - move it out of FilterController ). FilterRequest类设为静态(或者-更好的是-将其移出FilterController )。

Otherwise Jackson can't instantiate it (it would need an outer-instance of your parent class in order to construct an instance of the inner one). 否则,Jackson无法实例化它(它需要您的父类的外部实例才能构造内部实例的实例)。

If your class is declared as inner class, you need to make it static. 如果您的类被声明为内部类,则需要使其变为静态。 For instance : 例如 :

public class Application {

    private static final String requestBody = "{\"filters\": {\"test\": [\"hi\"],\"groups\": [],\"groupsOT\": [],\"chains\": [],\"chainsOT\": [],\"locations\": [],\"locationsOT\": [],\"reports\": [],\"reportsOT\": []},\"fromDate\": \"09.03.2015\",\"toDate\": \"16.03.2015\"}";


    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        FilterRequest requestParams = mapper.readValue(requestBody, FilterRequest.class);

        System.out.println(requestParams);
    }

    public static 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[]>();
        }

        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE);
        }
    }
}

Regards, 问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM