{"id":168,"date":"2024-12-05T11:08:22","date_gmt":"2024-12-05T16:08:22","guid":{"rendered":"https:\/\/coding101.xyz\/?p=168"},"modified":"2024-12-05T13:00:11","modified_gmt":"2024-12-05T18:00:11","slug":"resttemplate-example","status":"publish","type":"post","link":"https:\/\/coding101.xyz\/?p=168","title":{"rendered":"RestTemplate Example"},"content":{"rendered":"\n<p>This is a rest template example &#8211; how to exchange with a REST web service to get a certain type of data back. <\/p>\n\n\n\n<!--more-->\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">org.springframework.core.ParameterizedTypeReference<\/span><span style=\"color: #333333\">;<\/span>\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">org.springframework.http.*<\/span><span style=\"color: #333333\">;<\/span>\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">org.springframework.web.client.RestTemplate<\/span><span style=\"color: #333333\">;<\/span>\n\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">java.util.List<\/span><span style=\"color: #333333\">;<\/span>\n\n<span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #008800; font-weight: bold\">class<\/span> <span style=\"color: #BB0066; font-weight: bold\">RestTemplateExample<\/span> <span style=\"color: #333333\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #008800; font-weight: bold\">static<\/span> <span style=\"color: #333399; font-weight: bold\">void<\/span> <span style=\"color: #0066BB; font-weight: bold\">main<\/span><span style=\"color: #333333\">(<\/span>String<span style=\"color: #333333\">[]<\/span> args<span style=\"color: #333333\">)<\/span> <span style=\"color: #333333\">{<\/span>\n        RestTemplate restTemplate <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> RestTemplate<span style=\"color: #333333\">();<\/span>\n        String url <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;https:\/\/api.example.com\/items&quot;<\/span><span style=\"color: #333333\">;<\/span>\n\n        <span style=\"color: #888888\">\/\/ Create headers<\/span>\n        HttpHeaders headers <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> HttpHeaders<span style=\"color: #333333\">();<\/span>\n        headers<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">set<\/span><span style=\"color: #333333\">(<\/span><span style=\"background-color: #fff0f0\">&quot;Authorization&quot;<\/span><span style=\"color: #333333\">,<\/span> <span style=\"background-color: #fff0f0\">&quot;Bearer your_token_here&quot;<\/span><span style=\"color: #333333\">);<\/span>\n        headers<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">set<\/span><span style=\"color: #333333\">(<\/span><span style=\"background-color: #fff0f0\">&quot;Accept&quot;<\/span><span style=\"color: #333333\">,<\/span> <span style=\"background-color: #fff0f0\">&quot;application\/json&quot;<\/span><span style=\"color: #333333\">);<\/span>\n\n        <span style=\"color: #888888\">\/\/ Build the request entity with headers<\/span>\n        HttpEntity<span style=\"color: #333333\">&lt;<\/span>String<span style=\"color: #333333\">&gt;<\/span> requestEntity <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> HttpEntity<span style=\"color: #333333\">&lt;&gt;(<\/span>headers<span style=\"color: #333333\">);<\/span>\n\n        <span style=\"color: #888888\">\/\/ Make the exchange call<\/span>\n        ResponseEntity<span style=\"color: #333333\">&lt;<\/span>List<span style=\"color: #333333\">&lt;<\/span>ItemData<span style=\"color: #333333\">&gt;&gt;<\/span> response <span style=\"color: #333333\">=<\/span> restTemplate<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">exchange<\/span><span style=\"color: #333333\">(<\/span>\n                url<span style=\"color: #333333\">,<\/span>\n                HttpMethod<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">GET<\/span><span style=\"color: #333333\">,<\/span>\n                requestEntity<span style=\"color: #333333\">,<\/span>\n                <span style=\"color: #008800; font-weight: bold\">new<\/span> ParameterizedTypeReference<span style=\"color: #333333\">&lt;<\/span>List<span style=\"color: #333333\">&lt;<\/span>ItemData<span style=\"color: #333333\">&gt;&gt;()<\/span> <span style=\"color: #333333\">{}<\/span>\n        <span style=\"color: #333333\">);<\/span>\n\n        <span style=\"color: #888888\">\/\/ Get the response headers<\/span>\n        HttpHeaders responseHeaders <span style=\"color: #333333\">=<\/span> response<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">getHeaders<\/span><span style=\"color: #333333\">();<\/span>\n\n        <span style=\"color: #888888\">\/\/ Log response headers<\/span>\n        responseHeaders<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">forEach<\/span><span style=\"color: #333333\">((<\/span>key<span style=\"color: #333333\">,<\/span> value<span style=\"color: #333333\">)<\/span> <span style=\"color: #333333\">-&gt;<\/span> \n                System<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">out<\/span><span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">println<\/span><span style=\"color: #333333\">(<\/span>key <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;: &quot;<\/span> <span style=\"color: #333333\">+<\/span> String<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">join<\/span><span style=\"color: #333333\">(<\/span><span style=\"background-color: #fff0f0\">&quot;, &quot;<\/span><span style=\"color: #333333\">,<\/span> value<span style=\"color: #333333\">))<\/span>\n        <span style=\"color: #333333\">);<\/span>\n\n        <span style=\"color: #888888\">\/\/ Get the body<\/span>\n        List<span style=\"color: #333333\">&lt;<\/span>ItemData<span style=\"color: #333333\">&gt;<\/span> items <span style=\"color: #333333\">=<\/span> response<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">getBody<\/span><span style=\"color: #333333\">();<\/span>\n\n        <span style=\"color: #008800; font-weight: bold\">if<\/span> <span style=\"color: #333333\">(<\/span>items <span style=\"color: #333333\">!=<\/span> <span style=\"color: #008800; font-weight: bold\">null<\/span><span style=\"color: #333333\">)<\/span> <span style=\"color: #333333\">{<\/span>\n            items<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">forEach<\/span><span style=\"color: #333333\">(<\/span>System<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">out<\/span><span style=\"color: #333333\">::<\/span>println<span style=\"color: #333333\">);<\/span>\n        <span style=\"color: #333333\">}<\/span> <span style=\"color: #008800; font-weight: bold\">else<\/span> <span style=\"color: #333333\">{<\/span>\n            System<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">out<\/span><span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">println<\/span><span style=\"color: #333333\">(<\/span><span style=\"background-color: #fff0f0\">&quot;No items received.&quot;<\/span><span style=\"color: #333333\">);<\/span>\n        <span style=\"color: #333333\">}<\/span>\n    <span style=\"color: #333333\">}<\/span>\n<span style=\"color: #333333\">}<\/span>\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This is a rest template example &#8211; how to exchange with a REST web service to get a certain type of data back.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-168","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=168"}],"version-history":[{"count":4,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions"}],"predecessor-version":[{"id":175,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions\/175"}],"wp:attachment":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}