将响应映射到Java中的类

时间:2018-01-25 14:49:55

标签: java spring annotations

我正在开发新功能。 我想让那些允许检索特定端点数据的国家/地区。 请求返回此响应

{
    "response": {
        "mobile_web": {
                "countries": {
                    "at": {
                    "start_date": "2014-12",
                    "end_date": "2017-12",
                    "fresh_data": "2018-01-23"
                },
                "br": {
                    "start_date": "2014-12",
                    "end_date": "2017-12",
                    "fresh_data": "2018-01-23"
                },
                "id": {
                    "start_date": "2014-12",
                    "end_date": "2017-12",
                    "fresh_data": "2018-01-23"
                },
                "il": {
                    "start_date": "2014-12",
                    "end_date": "2017-12",
                    "fresh_data": "2018-01-23"
                },
                "in": {
                    "start_date": "2014-12",
                    "end_date": "2017-12",
                    "fresh_data": "2018-01-23"
                },
                "it": {
                    "start_date": "2014-12",
                    "end_date": "2017-12",
                    "fresh_data": "2018-01-23"
                },
                "jp": {
                    "start_date": "2014-12",
                    "end_date": "2017-12",
                    "fresh_data": "2018-01-23"
                },
                "ph": {
                    "start_date": "2014-12",
                    "end_date": "2017-12",
                    "fresh_data": "2018-01-23"
                },
                "pl": {
                    "start_date": "2014-12",
                    "end_date": "2017-12",
                    "fresh_data": "2018-01-23"
                },
                "pt": {
                    "start_date": "2014-12",
                    "end_date": "2017-12",
                    "fresh_data": "2018-01-23"
                },
                "ru": {
                    "start_date": "2014-12",
                    "end_date": "2017-12",
                    "fresh_data": "2018-01-23"
                },
                "th": {
                    "start_date": "2014-12",
                    "end_date": "2017-12",
                    "fresh_data": "2018-01-23"
                },
                "tr": {
                    "start_date": "2014-12",
                    "end_date": "2017-12",
                    "fresh_data": "2018-01-23"
                },
                "us": {
                    "start_date": "2014-12",
                    "end_date": "2017-12",
                    "fresh_data": "2018-01-23"
                },
                "world": {
                    "start_date": "2014-12",
                    "end_date": "2017-12",
                    "fresh_data": "2018-01-23"
                }
            }
        }
    }
}

我正在尝试将它映射到这个类:

@JsonIgnoreProperties(ignoreUnknown = true)
    public static class ReportResponse {

        @JsonProperty("response")
        public Result response;

        @JsonIgnoreProperties(ignoreUnknown = true)
        public static class Result {
            private Map<String, Object> result = new HashMap<>();

            @JsonAnyGetter
            public Map<String, Object> any() {
                return result;
            }

            @JsonAnySetter
            public void set(String name, Object value) {
                result.put(name, value);
            }
        }
    }

发送请求和接收响应的行是:

ReportResponse report = executeRequest(url, ReportResponse.class);

结果是: result

现在,我如何才能将国家/地区的地图作为报告?

1 个答案:

答案 0 :(得分:0)

问题在于内在阶级。将Result作为单独的类而不是内部类。一切都应该工作正常。为什么ReportResponse是静态的?是否在其他课程内?如果不是错的。

如果你需要使用内部类,那么你应该使用

你内心阶层的

@JsonAutoDetect(fieldVisibility = ANY)

确保所有领域都有适当的吸气剂和固定剂。