Skip to content

Commit 4623568

Browse files
committed
Polish client support for HTTP PATCH
Issue: SPR-7985
1 parent 8e56846 commit 4623568

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
package org.springframework.http.client;
1818

1919
import java.io.IOException;
20-
import java.lang.reflect.Constructor;
2120
import java.net.URI;
2221

2322
import org.apache.http.client.HttpClient;
2423
import org.apache.http.client.methods.HttpDelete;
2524
import org.apache.http.client.methods.HttpGet;
2625
import org.apache.http.client.methods.HttpHead;
2726
import org.apache.http.client.methods.HttpOptions;
27+
import org.apache.http.client.methods.HttpPatch;
2828
import org.apache.http.client.methods.HttpPost;
2929
import org.apache.http.client.methods.HttpPut;
3030
import org.apache.http.client.methods.HttpTrace;
@@ -56,6 +56,9 @@
5656
*/
5757
public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequestFactory, DisposableBean {
5858

59+
private static final boolean HTTP_PATCH_AVAILABLE = ClassUtils.isPresent(
60+
"org.apache.http.client.methods.HttpPatch", HttpComponentsClientHttpRequestFactory.class.getClassLoader());
61+
5962
private static final int DEFAULT_MAX_TOTAL_CONNECTIONS = 100;
6063

6164
private static final int DEFAULT_MAX_CONNECTIONS_PER_ROUTE = 5;
@@ -164,19 +167,12 @@ protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
164167
}
165168

166169
private HttpUriRequest createHttpPatch(URI uri) {
167-
String className = "org.apache.http.client.methods.HttpPatch";
168-
ClassLoader classloader = this.getClass().getClassLoader();
169-
if (!ClassUtils.isPresent(className, classloader)) {
170+
if (!HTTP_PATCH_AVAILABLE) {
170171
throw new IllegalArgumentException(
171172
"HTTP method PATCH not available before Apache HttpComponents HttpClient 4.2");
172173
}
173-
try {
174-
Class<?> clazz = classloader.loadClass(className);
175-
Constructor<?> constructor = clazz.getConstructor(URI.class);
176-
return (HttpUriRequest) constructor.newInstance(uri);
177-
}
178-
catch (Throwable ex) {
179-
throw new IllegalStateException("Unable to instantiate " + className, ex);
174+
else {
175+
return new HttpPatch(uri);
180176
}
181177
}
182178

spring-web/src/main/java/org/springframework/web/client/RestTemplate.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
* <tr><td>any</td><td>{@link #exchange}</td></tr>
7070
* <tr><td></td><td>{@link #execute}</td></tr> </table>
7171
*
72+
* <p>The {@code exchange} and {@code execute} methods are generalized versions of the more specific methods listed
73+
* above them. They support additional, less frequently used combinations including support for requests using the
74+
* HTTP PATCH method. However, note that the underlying HTTP library must also support the desired combination.</p>
75+
*
7276
* <p>For each of these HTTP methods, there are three corresponding Java methods in the {@code RestTemplate}. Two
7377
* variant take a {@code String} URI as first argument (eg. {@link #getForObject(String, Class, Object[])}, {@link
7478
* #getForObject(String, Class, Map)}), and are capable of substituting any {@linkplain UriTemplate URI templates} in

0 commit comments

Comments
 (0)