flip
flip

Reputation: 143

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. Here is the json i am trying to serialize. 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]

Upvotes: 1

Views: 2151

Answers (2)

user4584346
user4584346

Reputation:

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,

Upvotes: 0

David Lavender
David Lavender

Reputation: 8301

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.

Make the FilterRequest class static (or - better still - move it out of 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).

Upvotes: 1

Related Questions