Created
December 25, 2019 11:49
-
-
Save cenkc/8d61c1e600ac316c73362007f10ffe4d to your computer and use it in GitHub Desktop.
Spring RestTemplate usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cenkc.samples; | |
import org.springframework.http.*; | |
import org.springframework.web.client.RestTemplate; | |
import org.springframework.web.util.UriComponentsBuilder; | |
import java.net.URISyntaxException; | |
import java.util.Arrays; | |
public class RestTemplateSamples { | |
public static void main(String[] args) throws URISyntaxException { | |
/* | |
http://127.0.0.1/api/v1/poi/getSomeLocationInfo?lat=40.93563&lon=29.216923&radius=5000&limit=20 | |
*/ | |
String url = "http://127.0.0.1/api/v1/poi/getSomeLocationInfo"; | |
RestTemplate restTemplate = new RestTemplate(); | |
HttpHeaders headers = new HttpHeaders(); | |
headers.setContentType(MediaType.APPLICATION_XML); | |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML)); | |
HttpEntity<String> entity = new HttpEntity(null, headers); | |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url) | |
.queryParam("lat", 40.93563) | |
.queryParam("lon", 29.216923) | |
.queryParam("radius", 5000) | |
.queryParam("limit", 20); | |
// getForObject / getForEntity does not support request headers, use exchange() instead | |
ResponseEntity<String> responseEntity = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity, String.class); | |
if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { | |
System.out.println("responseEntity.getBody() = " + responseEntity.getBody()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment