resttemplate getForObject map responsetype

后端 未结 6 1748
囚心锁ツ
囚心锁ツ 2021-02-06 23:58

Update 02/05/2018 (about 4 years later)...I tested this again as people have been upvoting my question/answer and Sotirios Delimanolis is correct that I should not h

6条回答
  •  执念已碎
    2021-02-07 00:30

    Update 02/05/2018 (about 4 years later)...I tested this again as people have been upvoting my question/answer and Sotirios Delimanolis is correct that I should not have to write the code in my answer to make this work. I used basically the same RestTemplate/REST service setup as shown in my question with the REST service having a confirmed response content type of application/json and RestTemplate was able to process the response with no issues into a Map.


    I ended up getting the contents as a String and then converting them to a Map like this:

    String json = restTemplate.getForObject(buildUrl, String.class);
    Map map = new HashMap();
    ObjectMapper mapper = new ObjectMapper();
    
    try {
        //convert JSON string to Map
       map = mapper.readValue(json, new TypeReference>(){});
    } catch (Exception e) {
         logger.info("Exception converting {} to map", json, e);
    }
    
    return map;
    

提交回复
热议问题