resttemplate getForObject map responsetype

后端 未结 6 1744
囚心锁ツ
囚心锁ツ 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条回答
  •  -上瘾入骨i
    2021-02-07 00:35

    RestTemplate has a method named exchange that takes an instance of ParameterizedTypeReference as parameter.

    To make a GET request that returns a java.util.Map, just create an instance of an anonym class that inherits from ParameterizedTypeReference.

    ParameterizedTypeReference> responseType = 
                   new ParameterizedTypeReference>() {};
    

    You can then invoke the exchange method:

    RequestEntity request = RequestEntity.get(URI("http://example.com/foo"))
                     .accept(MediaType.APPLICATION_JSON).build()
    Map jsonDictionary = restTemplate.exchange(request, responseType)
    

提交回复
热议问题