From Spring Framework 4.3.5, patchForObject method (method for HTTP communication using PATCH method) has been added to RestTemplate, but if RestTemplate is used in the default state, an error will occur during communication processing. It ends up: sweat_smile:
This is because the class used in the default state is the JDK standard API java.net.HttpURLConnection. As some of you may know, HttpURLConnection doesn't support the PATCH method ...
Use an OSS library that supports the PATCH method! !!
Spring Framework supports the following OSS libraries.
All of the above libraries support PATCH methods.
The following steps are required.
ClientHttpRequestFactory corresponding to the OSS library into RestTemplateInjection example of ClientHttpRequestFactory
@Bean
RestTemplate restTemplate() {
    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
    return new RestTemplate(factory);
}
Note:
If you want to use RestTemplate on Spring Boot 1.4+, please see "Use RestTemplate (HTTP client) on Spring Boot 1.4+"! !! By generating
RestTemplateusingRestTemplateBuilderadded in Spring 1.4, 2 of the above step will be solved automatically.
Spring Framework provides the following implementation classes.
| name of the class | Description | 
|---|---|
| HttpComponentsClientHttpRequestFactory | Apache HttpComponents Implementation class for HTTP communication using HttpClient | 
| OkHttp3ClientHttpRequestFactory | Implementation class for HTTP communication using OkHttp 3 | 
| OkHttpClientHttpRequestFactory | Implementation class for HTTP communication using OkHttp 2 | 
| Netty4ClientHttpRequestFactory | Implementation class for HTTP communication using Netty 4 | 
| SimpleClientHttpRequestFactory | JDK HttpURLConnectionImplementation class for HTTP communication using (default implementation) | 
If you need to access the REST API (Web API) using the PATCH method ... Use the OSS library for HTTP communication! !!
Recommended Posts