通过Spring restTemplate从嵌套的JSON对象获取数组作为列表

时间:2019-05-22 18:35:48

标签: java spring-boot resttemplate

我有一个JSON作为REST HTTPGET答案。这是Here Map API创建的答案:

{
"response": {
    "metaInfo": {
        "timestamp": "2019-05-22T15:56:28Z",
        "mapVersion": "8.30.96.157",
        "moduleVersion": "7.2.201920-3525",
        "interfaceVersion": "2.6.56",
        "availableMapVersion": [
            "8.30.96.157"
        ]
    },
    "route": [
        {
            "waypoint": [

            ],
            "mode": {
                "type": "fastest",
                "transportModes": [
                    "car"
                ],
                "trafficMode": "disabled",
                "feature": []
            },
            "leg": [
                {
                    "start": {x
                    },
                    "end": {y
                    },
                    "length": 43301,
                    "travelTime": 2191,
                    "maneuver": [
                        {
                            "position": {
                                "latitude": ,
                                "longitude": 
                            },
                            "instruction": "bla",
                            "travelTime": 74,
                            "length": 387,
                            "id": "M1",
                            "_type": "PrivateTransportManeuverType"
                        },
                        {
                            "position": {
                                "latitude": ,
                                "longitude": 
                            },
                            "instruction": "",
                            "travelTime": 25,
                            "length": 12,
                            "id": "M2",
                            "_type": "PrivateTransportManeuverType"
                        },
                        {
                            "position": {
                                "latitude": ,
                                "longitude": 
                            },
                            "instruction": "",
                            "travelTime": 53,
                            "length": 515,
                            "id": "M3",
                            "_type": "PrivateTransportManeuverType"
                        },
                        {
                            "position": {
                                "latitude": ,
                                "longitude": 
                            },
                            "instruction": "",
                            "travelTime": 59,
                            "length": 309,
                            "id": "M4",
                            "_type": "PrivateTransportManeuverType"
                        },
                        {
                            "position": {
                                "latitude": ,
                                "longitude": 
                            },
                            "instruction": "Arrive at target",
                            "travelTime": 0,
                            "length": 0,
                            "id": "M19",
                            "_type": "PrivateTransportManeuverType"
                        }
                    ]
                }
            ],
            "summary": {
                "distance": 43301,
                "trafficTime": 2648,
                "baseTime": 2191,
                "flags": [
                    "tunnel",
                    "motorway",
                    "builtUpArea"
                ],
                "text": "",
                "travelTime": 2191,
                "_type": "RouteSummaryType"
            }
        }
    ],
    "language": "en-us"
}

}

(出于隐私和长度原因,我删除了15个航路点和坐标)

我想通过springboot应用程序和restTemplate获取嵌套数组“ maneuver”作为排序列表。

我知道如何到达对象。我已经使用过JSON2POJO映射器,并且具有所有可用的类。使用

@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
    return args -> {

        ResponseEntity<Response[]> responseEntity = restTemplate
                .getForEntity("https://route.api.here.com/routing/7.2/calculateroute.json?app_id=xjipTEag66SG6fNwgEQm&app_code=0eiHle7fs2IZsXChP2sWKQ&waypoint0=geo!" + start + "&waypoint1=geo!" + end + "&mode=fastest;car;traffic:disabled", Response[].class);
        Response[] objects = responseEntity.getBody();
        HttpStatus statusCode = responseEntity.getStatusCode();
        log.info("Status" + statusCode);
        Arrays.asList(objects).forEach(maneuver -> log.info(maneuver.toString()));
    };
}

给我一​​个反序列化错误。

@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
    return args -> {
        Response response = restTemplate.getForObject(
                "https://route.api.here.com/routing/7.2/calculateroute.json?app_id=xjipTEag66SG6fNwgEQm&app_code=0eiHle7fs2IZsXChP2sWKQ&waypoint0=geo!" + start + "&waypoint1=geo!" + end + "&mode=fastest;car;traffic:disabled", Response.class);
        log.info(response.toString());
    };

具有可以读取单个对象(如“ ID”)并将它们列出为一个字符串的功能:

2019-05-22 19:09:34.253  INFO 15928 --- [           main] trackr.GetRest                      : No active profile set, falling back to default profiles: default
2019-05-22 19:09:34.636  INFO 15928 --- [           main] trackr.GetRest                      : Started GetRest in 0.561 seconds (JVM running for 1.016)
2019-05-22 19:09:35.067  INFO 15928 --- [           main] klib.trackr.GetRest                      : [[[M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12, M13, M14, M15, M16, M17, M18, M19]]]

我对任何建议都很满意。我对Java和Spring restTemplate还是很陌生,我尝试了在StackOverflow上找到的不同解决方案,但是我使用它们的方式有误或无法正确理解。 非常感谢任何建议。

0 个答案:

没有答案