public class RestTemplate extends InterceptingHttpAccessor implements RestOperations
Spring is the central class for client-side synchronous HTTP access. This simplifies communication with HTTP servers and provides RESTful principles. It handles HTTP connections, leaving the application code to provide URLs (with possible template variables) and retrieve the results. Note. By default, RestTemplate uses the standard JDK features to establish HTTP connections. You can switch to using another HTTP protocol library such as Apache HttpComponents, Netty and OkHttp through the InterceptingHttpAccessor.setRequestFactory (org.springframework.http.client.ClientHttpRequestFactory) property.
The main entry points to this template are methods named after the six basic HTTP methods:
HTTP Method RestTemplate Methods
- DELETE delete (java.lang.String, java.lang.Object ...)
- GET getForObject (java.lang.String, java.lang.Class, java.lang.Object ...) getForEntity (java.lang.String, java.lang.Class, java.lang.Object ...)
- HEAD headForHeaders (java.lang.String, java.lang.Object ...)
- OPTIONS optionsForAllow (java.lang.String, java.lang.Object ...)
- POST postForLocation (java.lang.String, java.lang.Object, java.lang.Object ...) postForObject (java.lang.String, java.lang.Object, java.lang.Class, java.lang.Object. ..)
- PUT put (java.lang.String, java.lang.Object, java.lang.Object ...)
- any exchange (java.lang.String, org.springframework.http.HttpMethod, org.springframework.http.HttpEntity, java.lang.Class, java.lang.Object ...) execute (java.lang.String, org. springframework.http.HttpMethod, org.springframework.web.client.RequestCallback, org.springframework.web.client.ResponseExtractor, java.lang.Object ...)
RestTemplate
public class ResponseEntity<T> extends HttpEntity<T>
An HttpEntity extension that adds an HttpStatus status code. It is also used in RestTemplate @Controller methods.
ResponseEntity
Use RestTemplate methods and use ResponseEntity.
Code example:
@Override public ResponseEntity<?> callLocalGeo(double lat, double lng) { String url = "https://apis.sample.net/local/geo/coord2addr?apiKey=" + this.key + "&longitude=" + lng + "&latitude=" + lat + "&output=json"; RestTemplate rest = new RestTemplate(); try { // success return rest.getForEntity(url, String.class); } catch (HttpStatusCodeException e) { // new Error Response return ResponseEntity.status(e.getStatusCode()).headers(e.getResponseHeaders()) .body(e.getResponseBodyAsString()); } }
REST Call API
ResponseEntity<?> respEntity = sampleService.callLocalGeo(lat, lng); if (HttpStatus.OK.equals(respEntity.getStatusCode())) { String respBody = (String) respEntity.getBody();
RestTemplate Example
ClientHttpRequestInterceptor
Editing RestTemplate configuration.
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
Editing your interceptor.
@Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { ClientHttpResponse response = execution.execute(request, body); StringBuilder stringBuilder = new StringBuilder(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody(), "UTF-8")); String line = null; while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line); stringBuilder.append('\n'); }