diff --git a/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java b/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java
index cd729964f4d..86a74afe58c 100644
--- a/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java
+++ b/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2012 the original author or authors.
+ * Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,10 +17,8 @@
package org.springframework.web.client;
import java.io.IOException;
-import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URI;
-import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -51,12 +49,12 @@ import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.web.util.UriTemplate;
-import org.springframework.web.util.UriUtils;
/**
- * The central class for client-side HTTP access. It simplifies communication with HTTP servers, and
- * enforces RESTful principles. It handles HTTP connections, leaving application code to provide URLs (with possible
- * template variables) and extract results.
+ * Spring's central class for client-side HTTP access.
+ * It simplifies communication with HTTP servers, and enforces RESTful principles.
+ * It handles HTTP connections, leaving application code to provide URLs
+ * (with possible template variables) and extract results.
*
*
The main entry points of this template are the methods named after the six main HTTP methods:
*
@@ -74,7 +72,7 @@ import org.springframework.web.util.UriUtils;
*
* The {@code exchange} and {@code execute} methods are generalized versions of the more specific methods listed
* above them. They support additional, less frequently used combinations including support for requests using the
- * HTTP PATCH method. However, note that the underlying HTTP library must also support the desired combination.
+ * HTTP PATCH method. However, note that the underlying HTTP library must also support the desired combination.
*
* For each of these HTTP methods, there are three corresponding Java methods in the {@code RestTemplate}. Two
* variant take a {@code String} URI as first argument (eg. {@link #getForObject(String, Class, Object[])}, {@link
@@ -115,11 +113,11 @@ import org.springframework.web.util.UriUtils;
* requestFactory} and {@link #setErrorHandler(ResponseErrorHandler) errorHandler} bean properties.
*
* @author Arjen Poutsma
+ * @since 3.0
* @see HttpMessageConverter
* @see RequestCallback
* @see ResponseExtractor
* @see ResponseErrorHandler
- * @since 3.0
*/
public class RestTemplate extends InterceptingHttpAccessor implements RestOperations {
@@ -145,7 +143,9 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler();
- /** Create a new instance of the {@link RestTemplate} using default settings. */
+ /**
+ * Create a new instance of the {@link RestTemplate} using default settings.
+ */
public RestTemplate() {
this.messageConverters.add(new ByteArrayHttpMessageConverter());
this.messageConverters.add(new StringHttpMessageConverter());
@@ -171,7 +171,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
* Create a new instance of the {@link RestTemplate} based on the given {@link ClientHttpRequestFactory}.
* @param requestFactory HTTP request factory to use
* @see org.springframework.http.client.SimpleClientHttpRequestFactory
- * @see org.springframework.http.client.CommonsClientHttpRequestFactory
+ * @see org.springframework.http.client.HttpComponentsClientHttpRequestFactory
*/
public RestTemplate(ClientHttpRequestFactory requestFactory) {
this();
@@ -180,8 +180,8 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
/**
- * Set the message body converters to use. These converters are used to convert from and to HTTP requests and
- * responses.
+ * Set the message body converters to use.
+ *
> messageConverters) {
Assert.notEmpty(messageConverters, "'messageConverters' must not be empty");
@@ -311,17 +311,16 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
public ResponseEntity postForEntity(String url, Object request, Class responseType, Object... uriVariables)
throws RestClientException {
+
HttpEntityRequestCallback requestCallback = new HttpEntityRequestCallback(request, responseType);
ResponseEntityResponseExtractor responseExtractor =
new ResponseEntityResponseExtractor(responseType);
return execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables);
}
- public ResponseEntity postForEntity(String url,
- Object request,
- Class responseType,
- Map uriVariables)
+ public ResponseEntity postForEntity(String url, Object request, Class responseType, Map uriVariables)
throws RestClientException {
+
HttpEntityRequestCallback requestCallback = new HttpEntityRequestCallback(request, responseType);
ResponseEntityResponseExtractor responseExtractor =
new ResponseEntityResponseExtractor(responseType);
@@ -441,16 +440,14 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
public T execute(String url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor responseExtractor, Object... urlVariables) throws RestClientException {
- UriTemplate uriTemplate = new HttpUrlTemplate(url);
- URI expanded = uriTemplate.expand(urlVariables);
+ URI expanded = new UriTemplate(url).expand(urlVariables);
return doExecute(expanded, method, requestCallback, responseExtractor);
}
public T execute(String url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor responseExtractor, Map urlVariables) throws RestClientException {
- UriTemplate uriTemplate = new HttpUrlTemplate(url);
- URI expanded = uriTemplate.expand(urlVariables);
+ URI expanded = new UriTemplate(url).expand(urlVariables);
return doExecute(expanded, method, requestCallback, responseExtractor);
}
@@ -667,6 +664,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
}
}
+
/**
* Response extractor for {@link HttpEntity}.
*/
@@ -683,8 +681,8 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
}
public ResponseEntity extractData(ClientHttpResponse response) throws IOException {
- if (delegate != null) {
- T body = delegate.extractData(response);
+ if (this.delegate != null) {
+ T body = this.delegate.extractData(response);
return new ResponseEntity(body, response.getHeaders(), response.getStatusCode());
}
else {
@@ -704,30 +702,4 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
}
}
- /**
- * HTTP-specific subclass of UriTemplate, overriding the encode method.
- */
- @SuppressWarnings("serial")
- private static class HttpUrlTemplate extends UriTemplate {
-
- public HttpUrlTemplate(String uriTemplate) {
- super(uriTemplate);
- }
-
- @Override
- protected URI encodeUri(String uri) {
- try {
- String encoded = UriUtils.encodeHttpUrl(uri, "UTF-8");
- return new URI(encoded);
- }
- catch (UnsupportedEncodingException ex) {
- // should not happen, UTF-8 is always supported
- throw new IllegalStateException(ex);
- }
- catch (URISyntaxException ex) {
- throw new IllegalArgumentException("Could not create HTTP URL from [" + uri + "]: " + ex, ex);
- }
- }
- }
-
}
diff --git a/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java b/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java
index 3aacd605592..0f362ddf13f 100644
--- a/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java
+++ b/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java
@@ -71,6 +71,7 @@ public class UriTemplate implements Serializable {
this.uriComponents = UriComponentsBuilder.fromUriString(uriTemplate).build();
}
+
/**
* Return the names of the variables in the template, in order.
* @return the template variable names
@@ -79,8 +80,6 @@ public class UriTemplate implements Serializable {
return this.variableNames;
}
- // expanding
-
/**
* Given the Map of variables, expands this template into a URI. The Map keys represent variable names,
* the Map values variable values. The order of variables is not significant.
@@ -124,8 +123,6 @@ public class UriTemplate implements Serializable {
return encodedComponents.toUri();
}
- // matching
-
/**
* Indicate whether the given URI matches this template.
* @param uri the URI to match to
@@ -170,7 +167,7 @@ public class UriTemplate implements Serializable {
* Defaults to {@link UriUtils#encodeUri(String, String)}.
* @param uri the URI to encode
* @return the encoded URI
- * @deprecated No longer in use, with no direct replacement
+ * @deprecated No longer in use; to be removed in Spring 4.0.
*/
@Deprecated
protected URI encodeUri(String uri) {
@@ -216,7 +213,8 @@ public class UriTemplate implements Serializable {
}
else {
if (colonIdx + 1 == match.length()) {
- throw new IllegalArgumentException("No custom regular expression specified after ':' in \"" + match + "\"");
+ throw new IllegalArgumentException(
+ "No custom regular expression specified after ':' in \"" + match + "\"");
}
String variablePattern = match.substring(colonIdx + 1, match.length());
this.patternBuilder.append('(');
@@ -250,5 +248,4 @@ public class UriTemplate implements Serializable {
}
}
-
}