Add resttemplate-to-restclient module: RestTemplate deprecation on Spring Framework 7.1 / Boot 4.2.0 vs 4.1.3, method mapping, exchange() trap, bridge URI encoding, RestTestClient, OpenRewrite recipe run, with real transcripts

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b7M7FEdeEm3APj16Bu
This commit is contained in:
Claude
2026-11-21 21:26:48 +05:30
parent 5f85ec8d53
commit cbcfb05e35
77 changed files with 2187 additions and 0 deletions
@@ -0,0 +1,57 @@
# 01-method-mapping (Spring Boot 4.1.3, JDK 25)
server: StudentController on http://localhost:<random port>, seeded with 1=Asha, 2=Ravi
RestTemplate getForObject(url, Student.class, 1)
-> Student[id=1, name=Asha]
RestClient get().uri(url, 1).retrieve().body(Student.class)
-> Student[id=1, name=Asha] same result: true
RestTemplate getForEntity(url, Student.class, 2)
-> 200 OK application/json Student[id=2, name=Ravi]
RestClient get().uri(url, 2).retrieve().toEntity(Student.class)
-> 200 OK application/json Student[id=2, name=Ravi] same result: true
RestTemplate postForObject(url, request, Student.class)
-> Student[id=3, name=Meera]
RestClient post().uri(url).body(request).retrieve().body(Student.class)
-> Student[id=3, name=Meera] same result: true
RestTemplate postForEntity(url, request, Student.class)
-> 201 CREATED /students/3 Student[id=3, name=Kabir]
RestClient post().uri(url).body(request).retrieve().toEntity(Student.class)
-> 201 CREATED /students/3 Student[id=3, name=Kabir] same result: true
RestTemplate postForLocation(url, request)
-> /students/3
RestClient post().uri(url).body(request).retrieve().toBodilessEntity().getHeaders().getLocation()
-> /students/3 same result: true
RestTemplate put(url, request, 1)
-> Asha K
RestClient put().uri(url, 1).body(request).retrieve().toBodilessEntity()
-> Asha K (status 204 NO_CONTENT) same result: true
RestTemplate delete(url, 1)
-> then GET -> 404
RestClient delete().uri(url, 1).retrieve().toBodilessEntity()
-> then GET -> 404 (delete status 204 NO_CONTENT) same result: true
RestTemplate headForHeaders(url, 1) content-type
-> application/json
RestClient head().uri(url, 1).retrieve().toBodilessEntity().getHeaders()
-> application/json same result: true
RestTemplate optionsForAllow(url)
-> [GET, HEAD, OPTIONS, POST]
RestClient options().uri(url).retrieve().toBodilessEntity().getHeaders().getAllow()
-> [GET, HEAD, OPTIONS, POST] same result: true
RestTemplate exchange(url, GET, new HttpEntity<>(headers), new ParameterizedTypeReference<List<Student>>() {})
-> [Student[id=1, name=Asha], Student[id=2, name=Ravi]]
RestClient get().uri(url).headers(h -> ...).retrieve().body(new ParameterizedTypeReference<List<Student>>() {})
-> [Student[id=1, name=Asha], Student[id=2, name=Ravi]] same result: true
server saw Authorization on both: Bearer tok-123
LegacyStudentClient.findById(1) == StudentClient.findById(1): true
LegacyStudentClient.findAll(token) == StudentClient.findAll(token): true
@@ -0,0 +1,57 @@
# 01-method-mapping (Spring Boot 4.2.0, JDK 25)
server: StudentController on http://localhost:<random port>, seeded with 1=Asha, 2=Ravi
RestTemplate getForObject(url, Student.class, 1)
-> Student[id=1, name=Asha]
RestClient get().uri(url, 1).retrieve().body(Student.class)
-> Student[id=1, name=Asha] same result: true
RestTemplate getForEntity(url, Student.class, 2)
-> 200 OK application/json Student[id=2, name=Ravi]
RestClient get().uri(url, 2).retrieve().toEntity(Student.class)
-> 200 OK application/json Student[id=2, name=Ravi] same result: true
RestTemplate postForObject(url, request, Student.class)
-> Student[id=3, name=Meera]
RestClient post().uri(url).body(request).retrieve().body(Student.class)
-> Student[id=3, name=Meera] same result: true
RestTemplate postForEntity(url, request, Student.class)
-> 201 CREATED /students/3 Student[id=3, name=Kabir]
RestClient post().uri(url).body(request).retrieve().toEntity(Student.class)
-> 201 CREATED /students/3 Student[id=3, name=Kabir] same result: true
RestTemplate postForLocation(url, request)
-> /students/3
RestClient post().uri(url).body(request).retrieve().toBodilessEntity().getHeaders().getLocation()
-> /students/3 same result: true
RestTemplate put(url, request, 1)
-> Asha K
RestClient put().uri(url, 1).body(request).retrieve().toBodilessEntity()
-> Asha K (status 204 NO_CONTENT) same result: true
RestTemplate delete(url, 1)
-> then GET -> 404
RestClient delete().uri(url, 1).retrieve().toBodilessEntity()
-> then GET -> 404 (delete status 204 NO_CONTENT) same result: true
RestTemplate headForHeaders(url, 1) content-type
-> application/json
RestClient head().uri(url, 1).retrieve().toBodilessEntity().getHeaders()
-> application/json same result: true
RestTemplate optionsForAllow(url)
-> [GET, HEAD, OPTIONS, POST]
RestClient options().uri(url).retrieve().toBodilessEntity().getHeaders().getAllow()
-> [GET, HEAD, OPTIONS, POST] same result: true
RestTemplate exchange(url, GET, new HttpEntity<>(headers), new ParameterizedTypeReference<List<Student>>() {})
-> [Student[id=1, name=Asha], Student[id=2, name=Ravi]]
RestClient get().uri(url).headers(h -> ...).retrieve().body(new ParameterizedTypeReference<List<Student>>() {})
-> [Student[id=1, name=Asha], Student[id=2, name=Ravi]] same result: true
server saw Authorization on both: Bearer tok-123
LegacyStudentClient.findById(1) == StudentClient.findById(1): true
LegacyStudentClient.findAll(token) == StudentClient.findAll(token): true
@@ -0,0 +1,17 @@
# 02-error-defaults (Spring Boot 4.1.3, JDK 25)
GET /students/99 (404)
RestTemplate : org.springframework.web.client.HttpClientErrorException$NotFound
RestClient : org.springframework.web.client.HttpClientErrorException$NotFound
RestTemplate message : 404 on GET request for "http://localhost:<port>/students/99": "{"timestamp":"<time>","status":404,"error":"Not Found","path":"/students/99"}"
RestClient message : 404 Not Found: "{"timestamp":"<time>","status":404,"error":"Not Found","path":"/students/99"}"
same message: false
GET /boom (500)
RestTemplate : org.springframework.web.client.HttpServerErrorException$InternalServerError
RestClient : org.springframework.web.client.HttpServerErrorException$InternalServerError
RestTemplate message : 500 on GET request for "http://localhost:<port>/boom": "{"timestamp":"<time>","status":500,"error":"Internal Server Error","path":"/boom"}"
RestClient message : 500 Internal Server Error: "{"timestamp":"<time>","status":500,"error":"Internal Server Error","path":"/boom"}"
same message: false
connection refused (nothing listening)
RestTemplate : org.springframework.web.client.ResourceAccessException <- cause java.net.ConnectException
RestClient : org.springframework.web.client.ResourceAccessException <- cause java.net.ConnectException
204 No Content body: RestTemplate.getForObject -> null, RestClient.body(String.class) -> null
@@ -0,0 +1,17 @@
# 02-error-defaults (Spring Boot 4.2.0, JDK 25)
GET /students/99 (404)
RestTemplate : org.springframework.web.client.HttpClientErrorException$NotFound
RestClient : org.springframework.web.client.HttpClientErrorException$NotFound
RestTemplate message : 404 on GET request for "http://localhost:<port>/students/99": "{"timestamp":"<time>","status":404,"error":"Not Found","path":"/students/99"}"
RestClient message : 404 Not Found: "{"timestamp":"<time>","status":404,"error":"Not Found","path":"/students/99"}"
same message: false
GET /boom (500)
RestTemplate : org.springframework.web.client.HttpServerErrorException$InternalServerError
RestClient : org.springframework.web.client.HttpServerErrorException$InternalServerError
RestTemplate message : 500 on GET request for "http://localhost:<port>/boom": "{"timestamp":"<time>","status":500,"error":"Internal Server Error","path":"/boom"}"
RestClient message : 500 Internal Server Error: "{"timestamp":"<time>","status":500,"error":"Internal Server Error","path":"/boom"}"
same message: false
connection refused (nothing listening)
RestTemplate : org.springframework.web.client.ResourceAccessException <- cause java.net.ConnectException
RestClient : org.springframework.web.client.ResourceAccessException <- cause java.net.ConnectException
204 No Content body: RestTemplate.getForObject -> null, RestClient.body(String.class) -> null
@@ -0,0 +1,10 @@
# 03-error-handler-migration (Spring Boot 4.1.3, JDK 25)
GET /students/99 with a custom 404 translation
RestTemplate.setErrorHandler(handler) -> StudentNotFoundException: no such resource: /students/99
builder.defaultStatusHandler(<same handler>) -> StudentNotFoundException: no such resource: /students/99
builder.defaultStatusHandler(predicate, lambda) -> StudentNotFoundException: no such resource: /students/99
retrieve().onStatus(predicate, lambda) per call -> StudentNotFoundException: no such resource: /students/99
GET /boom (500), not claimed by the custom handler
RestTemplate -> org.springframework.web.client.HttpServerErrorException$InternalServerError
defaultStatusHandler(<handler>) -> org.springframework.web.client.HttpServerErrorException$InternalServerError
defaultStatusHandler(pred, lambda)-> org.springframework.web.client.HttpServerErrorException$InternalServerError
@@ -0,0 +1,10 @@
# 03-error-handler-migration (Spring Boot 4.2.0, JDK 25)
GET /students/99 with a custom 404 translation
RestTemplate.setErrorHandler(handler) -> StudentNotFoundException: no such resource: /students/99
builder.defaultStatusHandler(<same handler>) -> StudentNotFoundException: no such resource: /students/99
builder.defaultStatusHandler(predicate, lambda) -> StudentNotFoundException: no such resource: /students/99
retrieve().onStatus(predicate, lambda) per call -> StudentNotFoundException: no such resource: /students/99
GET /boom (500), not claimed by the custom handler
RestTemplate -> org.springframework.web.client.HttpServerErrorException$InternalServerError
defaultStatusHandler(<handler>) -> org.springframework.web.client.HttpServerErrorException$InternalServerError
defaultStatusHandler(pred, lambda)-> org.springframework.web.client.HttpServerErrorException$InternalServerError
@@ -0,0 +1,7 @@
# 04-exchange-trap (Spring Boot 4.1.3, JDK 25)
RestTemplate.exchange(GET /students/99) -> throws NotFound
RestClient.get()...retrieve().body(...) -> throws NotFound
RestClient.get()...exchange((req, res) -> res.bodyTo(Student.class))
-> throws: nothing; returned Student[id=null, name=null]
RestClient.get()...exchange(...) on GET /boom -> no exception; handler saw 500 INTERNAL_SERVER_ERROR {"timestamp":"<time>","status":500,"error":"Internal Server Error","path":"/boom"}
exchange(...) with an explicit isError() check -> IllegalStateException: HTTP 404 for /students/99
@@ -0,0 +1,7 @@
# 04-exchange-trap (Spring Boot 4.2.0, JDK 25)
RestTemplate.exchange(GET /students/99) -> throws NotFound
RestClient.get()...retrieve().body(...) -> throws NotFound
RestClient.get()...exchange((req, res) -> res.bodyTo(Student.class))
-> throws: nothing; returned Student[id=null, name=null]
RestClient.get()...exchange(...) on GET /boom -> no exception; handler saw 500 INTERNAL_SERVER_ERROR {"timestamp":"<time>","status":500,"error":"Internal Server Error","path":"/boom"}
exchange(...) with an explicit isError() check -> IllegalStateException: HTTP 404 for /students/99
@@ -0,0 +1,6 @@
# 05-bridge-what-is-copied (Spring Boot 4.1.3, JDK 25)
GET /whoami, what the server received
RestTemplate(JdkClientHttpRequestFactory + interceptor): x-request-id=abc-123 user-agent=Java-http-client/25.0.5.1
RestClient.create(thatTemplate) : x-request-id=abc-123 user-agent=Java-http-client/25.0.5.1
RestClient.create() (no bridge) : x-request-id=null user-agent=Java-http-client/25.0.5.1
GET /students/99 through the bridge, template had a custom ResponseErrorHandler -> StudentNotFoundException
@@ -0,0 +1,6 @@
# 05-bridge-what-is-copied (Spring Boot 4.2.0, JDK 25)
GET /whoami, what the server received
RestTemplate(JdkClientHttpRequestFactory + interceptor): x-request-id=abc-123 user-agent=Java-http-client/25.0.5.1
RestClient.create(thatTemplate) : x-request-id=abc-123 user-agent=Java-http-client/25.0.5.1
RestClient.create() (no bridge) : x-request-id=null user-agent=Java-http-client/25.0.5.1
GET /students/99 through the bridge, template had a custom ResponseErrorHandler -> StudentNotFoundException
@@ -0,0 +1,9 @@
# 06-bridge-uri-encoding (Spring Boot 4.1.3, JDK 25)
RestTemplate default URI encoding mode : URI_COMPONENT
RestClient default URI encoding mode : TEMPLATE_AND_VALUES (DefaultUriBuilderFactory default)
variable q = "C++", template "/search?q={q}"
new RestTemplate() wire: q=C++ server decoded q = "C "
RestClient.create(restTemplate) wire: q=C%2B%2B server decoded q = "C++"
RestClient.create() wire: q=C%2B%2B server decoded q = "C++"
builder(rt).uriBuilderFactory(URI_COMPONENT) wire: q=C++ server decoded q = "C "
@@ -0,0 +1,9 @@
# 06-bridge-uri-encoding (Spring Boot 4.2.0, JDK 25)
RestTemplate default URI encoding mode : URI_COMPONENT
RestClient default URI encoding mode : TEMPLATE_AND_VALUES (DefaultUriBuilderFactory default)
variable q = "C++", template "/search?q={q}"
new RestTemplate() wire: q=C++ server decoded q = "C "
RestClient.create(restTemplate) wire: q=C%2B%2B server decoded q = "C++"
RestClient.create() wire: q=C%2B%2B server decoded q = "C++"
builder(rt).uriBuilderFactory(URI_COMPONENT) wire: q=C++ server decoded q = "C "
@@ -0,0 +1,14 @@
# 07-default-request-factories (Spring Boot 4.1.3, JDK 25)
request factory class on RestTemplate: new RestTemplate() -> org.springframework.http.client.SimpleClientHttpRequestFactory
request factory class on RestTemplate: RestTemplateBuilder.build() -> org.springframework.http.client.JdkClientHttpRequestFactory
User-Agent the server received (GET /whoami)
new RestTemplate() : Java/25.0.5.1
RestClient.create() : Java-http-client/25.0.5.1
injected RestClient.Builder (Boot) : Java-http-client/25.0.5.1
RestTemplateBuilder.build() (Boot) : Java-http-client/25.0.5.1
PATCH /students/1
new RestTemplate().patchForObject(...) -> ResourceAccessException: I/O error on PATCH request for "http://localhost:<port>/students/1": Invalid HTTP method: PATCH
RestClient.create().patch()...body(Student) -> Student[id=1, name=Asha P]
new RestTemplate(new JdkClientHttpRequestFactory()).patchForObject(...) -> Student[id=1, name=Asha Q]
@@ -0,0 +1,14 @@
# 07-default-request-factories (Spring Boot 4.2.0, JDK 25)
request factory class on RestTemplate: new RestTemplate() -> org.springframework.http.client.SimpleClientHttpRequestFactory
request factory class on RestTemplate: RestTemplateBuilder.build() -> org.springframework.http.client.JdkClientHttpRequestFactory
User-Agent the server received (GET /whoami)
new RestTemplate() : Java/25.0.5.1
RestClient.create() : Java-http-client/25.0.5.1
injected RestClient.Builder (Boot) : Java-http-client/25.0.5.1
RestTemplateBuilder.build() (Boot) : Java-http-client/25.0.5.1
PATCH /students/1
new RestTemplate().patchForObject(...) -> ResourceAccessException: I/O error on PATCH request for "http://localhost:<port>/students/1": Invalid HTTP method: PATCH
RestClient.create().patch()...body(Student) -> Student[id=1, name=Asha P]
new RestTemplate(new JdkClientHttpRequestFactory()).patchForObject(...) -> Student[id=1, name=Asha Q]
@@ -0,0 +1,7 @@
# 08-interceptor-reads-body (Spring Boot 4.1.3, JDK 25)
GET /text (server body: "plain text body"); the interceptor reads the body to log it
RestTemplate, default factory -> caller got null
RestTemplate + BufferingClientHttpRequestFactory -> caller got "plain text body"
RestClient, interceptor only -> caller got null
RestClient + bufferContent((uri, method) -> true) -> caller got "plain text body"
interceptor logged: [plain text body, plain text body, plain text body, plain text body]
@@ -0,0 +1,7 @@
# 08-interceptor-reads-body (Spring Boot 4.2.0, JDK 25)
GET /text (server body: "plain text body"); the interceptor reads the body to log it
RestTemplate, default factory -> caller got null
RestTemplate + BufferingClientHttpRequestFactory -> caller got "plain text body"
RestClient, interceptor only -> caller got null
RestClient + bufferContent((uri, method) -> true) -> caller got "plain text body"
interceptor logged: [plain text body, plain text body, plain text body, plain text body]
@@ -0,0 +1,8 @@
# 09-timeouts (Spring Boot 4.1.3, JDK 25)
server: GET /slow?ms=1500 sleeps 1500 ms
application.properties equivalent: spring.http.clients.connect-timeout=300ms, spring.http.clients.read-timeout=300ms
injected RestClient.Builder (Boot properties) -> ResourceAccessException <- java.net.http.HttpTimeoutException, gave up after roughly 300-1200 ms
RestTemplateBuilder.build() (same Boot properties) -> ResourceAccessException <- java.net.http.HttpTimeoutException, gave up after roughly 300-1200 ms
RestClient.builder().requestFactory(detect().build(HttpClientSettings.defaults().withReadTimeout(300ms))) -> ResourceAccessException <- java.net.http.HttpTimeoutException, gave up after roughly 300-1200 ms
RestClient.create() (no timeouts configured) -> "done after 1500 ms", >1200 ms
@@ -0,0 +1,8 @@
# 09-timeouts (Spring Boot 4.2.0, JDK 25)
server: GET /slow?ms=1500 sleeps 1500 ms
application.properties equivalent: spring.http.clients.connect-timeout=300ms, spring.http.clients.read-timeout=300ms
injected RestClient.Builder (Boot properties) -> ResourceAccessException <- java.net.http.HttpTimeoutException, gave up after roughly 300-1200 ms
RestTemplateBuilder.build() (same Boot properties) -> ResourceAccessException <- java.net.http.HttpTimeoutException, gave up after roughly 300-1200 ms
RestClient.builder().requestFactory(detect().build(HttpClientSettings.defaults().withReadTimeout(300ms))) -> ResourceAccessException <- java.net.http.HttpTimeoutException, gave up after roughly 300-1200 ms
RestClient.create() (no timeouts configured) -> "done after 1500 ms", >1200 ms
@@ -0,0 +1,9 @@
# 10-test-clients (Spring Boot 4.1.3, JDK 25)
TestRestTemplate.getForEntity("/students/1", Student.class) -> 200 OK Student[id=1, name=Asha]
RestTestClient.get().uri("/students/1").exchange().expectStatus().isOk().expectBody(Student.class) -> Student[id=1, name=Asha]
TestRestTemplate on a 404 does not throw: status 404 NOT_FOUND
RestTestClient on a 404 does not throw: expectStatus().isNotFound() passed
TestRestTemplate.postForEntity(...) -> 201 CREATED Location /students/3
RestTestClient.post()...body(...).exchange().expectStatus().isCreated().expectHeader().exists("Location") passed
TestRestTemplate.withBasicAuth("user", "pw") -> server saw Authorization: Basic dXNlcjpwdw==
RestTestClient.mutate().defaultHeaders(h -> h.setBasicAuth("user", "pw")) -> server saw Authorization: Basic dXNlcjpwdw==
@@ -0,0 +1,9 @@
# 10-test-clients (Spring Boot 4.2.0, JDK 25)
TestRestTemplate.getForEntity("/students/1", Student.class) -> 200 OK Student[id=1, name=Asha]
RestTestClient.get().uri("/students/1").exchange().expectStatus().isOk().expectBody(Student.class) -> Student[id=1, name=Asha]
TestRestTemplate on a 404 does not throw: status 404 NOT_FOUND
RestTestClient on a 404 does not throw: expectStatus().isNotFound() passed
TestRestTemplate.postForEntity(...) -> 201 CREATED Location /students/3
RestTestClient.post()...body(...).exchange().expectStatus().isCreated().expectHeader().exists("Location") passed
TestRestTemplate.withBasicAuth("user", "pw") -> server saw Authorization: Basic dXNlcjpwdw==
RestTestClient.mutate().defaultHeaders(h -> h.setBasicAuth("user", "pw")) -> server saw Authorization: Basic dXNlcjpwdw==
@@ -0,0 +1,3 @@
# 11-mock-server (Spring Boot 4.1.3, JDK 25)
MockRestServiceServer.bindTo(restTemplate) -> Student[id=1, name=Asha]
MockRestServiceServer.bindTo(restClientBuilder) -> Student[id=1, name=Asha]
@@ -0,0 +1,3 @@
# 11-mock-server (Spring Boot 4.2.0, JDK 25)
MockRestServiceServer.bindTo(restTemplate) -> Student[id=1, name=Asha]
MockRestServiceServer.bindTo(restClientBuilder) -> Student[id=1, name=Asha]
@@ -0,0 +1,5 @@
# 12-auto-configured-beans (Spring Boot 4.1.3, JDK 25)
RestClient.Builder beans : 1
RestTemplateBuilder beans: 1
RestTemplate beans : 0 (Boot never registers a RestTemplate itself)
two getBean(RestClient.Builder) calls return the same instance: false (prototype scope: configure your copy freely)
@@ -0,0 +1,5 @@
# 12-auto-configured-beans (Spring Boot 4.2.0, JDK 25)
RestClient.Builder beans : 1
RestTemplateBuilder beans: 1
RestTemplate beans : 0 (Boot never registers a RestTemplate itself)
two getBean(RestClient.Builder) calls return the same instance: false (prototype scope: configure your copy freely)
@@ -0,0 +1,3 @@
# 13-resttemplate-compiler-warnings (Spring Boot 4.1.3, JDK 25.0.5.1)
$ javac -Xlint:deprecation -cp <module classpath> -d target/broken src/broken/UsesRestTemplate.java
exit: 0
@@ -0,0 +1,10 @@
# 13-resttemplate-compiler-warnings (Spring Boot 4.2.0, JDK 25.0.5.1)
$ javac -Xlint:deprecation -cp <module classpath> -d target/broken src/broken/UsesRestTemplate.java
src/broken/UsesRestTemplate.java:11: warning: [removal] RestTemplateBuilder in org.springframework.boot.restclient has been deprecated and marked for removal
public String fetch(RestTemplateBuilder builder) {
^
src/broken/UsesRestTemplate.java:12: warning: [removal] RestTemplate in org.springframework.web.client has been deprecated and marked for removal
RestTemplate rest = builder.build();
^
2 warnings
exit: 0
@@ -0,0 +1,3 @@
# 14-testresttemplate-compiler-warnings (Spring Boot 4.1.3, JDK 25.0.5.1)
$ javac -Xlint:deprecation -cp <module classpath> -d target/broken src/broken/UsesTestRestTemplate.java
exit: 0
@@ -0,0 +1,7 @@
# 14-testresttemplate-compiler-warnings (Spring Boot 4.2.0, JDK 25.0.5.1)
$ javac -Xlint:deprecation -cp <module classpath> -d target/broken src/broken/UsesTestRestTemplate.java
src/broken/UsesTestRestTemplate.java:8: warning: [removal] TestRestTemplate in org.springframework.boot.resttestclient has been deprecated and marked for removal
public String fetch(TestRestTemplate rest) {
^
1 warning
exit: 0
@@ -0,0 +1,19 @@
# 15-old-timeout-settings-compiler-error (Spring Boot 4.1.3, JDK 25.0.5.1)
$ javac -Xlint:deprecation -cp <module classpath> -d target/broken src/broken/OldTimeoutSettings.java
src/broken/OldTimeoutSettings.java:4: error: cannot find symbol
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
^
symbol: class ClientHttpRequestFactorySettings
location: package org.springframework.boot.http.client
src/broken/OldTimeoutSettings.java:14: error: cannot find symbol
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.defaults()
^
symbol: class ClientHttpRequestFactorySettings
location: class OldTimeoutSettings
src/broken/OldTimeoutSettings.java:14: error: cannot find symbol
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.defaults()
^
symbol: variable ClientHttpRequestFactorySettings
location: class OldTimeoutSettings
3 errors
exit: 1
@@ -0,0 +1,19 @@
# 15-old-timeout-settings-compiler-error (Spring Boot 4.2.0, JDK 25.0.5.1)
$ javac -Xlint:deprecation -cp <module classpath> -d target/broken src/broken/OldTimeoutSettings.java
src/broken/OldTimeoutSettings.java:4: error: cannot find symbol
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
^
symbol: class ClientHttpRequestFactorySettings
location: package org.springframework.boot.http.client
src/broken/OldTimeoutSettings.java:14: error: cannot find symbol
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.defaults()
^
symbol: class ClientHttpRequestFactorySettings
location: class OldTimeoutSettings
src/broken/OldTimeoutSettings.java:14: error: cannot find symbol
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.defaults()
^
symbol: variable ClientHttpRequestFactorySettings
location: class OldTimeoutSettings
3 errors
exit: 1
@@ -0,0 +1,3 @@
# 16-new-timeout-settings-compiler (Spring Boot 4.1.3, JDK 25.0.5.1)
$ javac -Xlint:deprecation -cp <module classpath> -d target/broken src/broken/NewTimeoutSettings.java
exit: 0
@@ -0,0 +1,3 @@
# 16-new-timeout-settings-compiler (Spring Boot 4.2.0, JDK 25.0.5.1)
$ javac -Xlint:deprecation -cp <module classpath> -d target/broken src/broken/NewTimeoutSettings.java
exit: 0
@@ -0,0 +1,3 @@
# 17-migrated-client-compiler (Spring Boot 4.1.3, JDK 25.0.5.1)
$ javac -Xlint:deprecation -cp <module classpath> -d target/broken src/main/java/com/ankurm/rtmigration/StudentClient.java
exit: 0
@@ -0,0 +1,3 @@
# 17-migrated-client-compiler (Spring Boot 4.2.0, JDK 25.0.5.1)
$ javac -Xlint:deprecation -cp <module classpath> -d target/broken src/main/java/com/ankurm/rtmigration/StudentClient.java
exit: 0
@@ -0,0 +1,3 @@
# 18-legacy-client-without-suppression (Spring Boot 4.1.3, JDK 25.0.5.1)
$ javac -Xlint:deprecation -cp <module classpath> -d target/broken target/LegacyStudentClient.java
exit: 0
@@ -0,0 +1,10 @@
# 18-legacy-client-without-suppression (Spring Boot 4.2.0, JDK 25.0.5.1)
$ javac -Xlint:deprecation -cp <module classpath> -d target/broken target/LegacyStudentClient.java
target/LegacyStudentClient.java:19: warning: [removal] RestTemplate in org.springframework.web.client has been deprecated and marked for removal
private final RestTemplate restTemplate;
^
target/LegacyStudentClient.java:23: warning: [removal] RestTemplate in org.springframework.web.client has been deprecated and marked for removal
public LegacyStudentClient(RestTemplate restTemplate, String base) {
^
2 warnings
exit: 0
@@ -0,0 +1,25 @@
# 19-deprecation-annotations (javap -v on the published jars, JDK 25.0.5.1)
spring-web 7.0.11 org.springframework.web.client.RestTemplate : not deprecated
spring-web 7.0.11 org.springframework.web.client.RestClient : not deprecated
spring-web 7.1.0 org.springframework.web.client.RestTemplate : since="7.1" forRemoval=true
spring-web 7.1.0 org.springframework.web.client.RestClient : not deprecated
spring-boot-restclient 4.1.3 RestTemplateBuilder : not deprecated
spring-boot-restclient 4.1.3 RestTemplateCustomizer : not deprecated
spring-boot-restclient 4.1.3 RestClientCustomizer : not deprecated
spring-boot-restclient 4.1.3 autoconfigure.RestTemplateAutoConfiguration : not deprecated
spring-boot-restclient 4.1.3 autoconfigure.RestClientAutoConfiguration : not deprecated
spring-boot-resttestclient 4.1.3 TestRestTemplate : not deprecated
spring-boot-resttestclient 4.1.3 autoconfigure.AutoConfigureTestRestTemplate : not deprecated
spring-boot-resttestclient 4.1.3 autoconfigure.AutoConfigureRestTestClient : not deprecated
spring-boot-restclient 4.2.0 RestTemplateBuilder : since="4.2.0" forRemoval=true
spring-boot-restclient 4.2.0 RestTemplateCustomizer : since="4.2.0" forRemoval=true
spring-boot-restclient 4.2.0 RestClientCustomizer : not deprecated
spring-boot-restclient 4.2.0 autoconfigure.RestTemplateAutoConfiguration : since="4.2.0" forRemoval=true
spring-boot-restclient 4.2.0 autoconfigure.RestClientAutoConfiguration : not deprecated
spring-boot-resttestclient 4.2.0 TestRestTemplate : since="4.2.0" forRemoval=true
spring-boot-resttestclient 4.2.0 autoconfigure.AutoConfigureTestRestTemplate : since="4.2.0" forRemoval=true
spring-boot-resttestclient 4.2.0 autoconfigure.AutoConfigureRestTestClient : not deprecated
@@ -0,0 +1,23 @@
# 20-openrewrite-diff (io.moderne.recipe:rewrite-spring 0.40.1, rewrite-maven-plugin 6.46.1, Spring Boot 4.2.0)
$ mvn rewrite:run -Drewrite.activeRecipes=io.moderne.java.spring.boot3.MigrateRestTemplateToRestClient
@@ -12 +12 @@ import org.springframework.http.ResponseEntity;
-import org.springframework.web.client.RestTemplate;
+import org.springframework.web.client.RestClient;
@@ -20 +20 @@ public class RemoteClient {
- private final RestTemplate restTemplate = new RestTemplate();
+ private final RestClient restTemplate = RestClient.create();
@@ -29 +29 @@ public class RemoteClient {
- return this.restTemplate.getForObject(this.base + "/students/{id}", Student.class, id);
+ return this.restTemplate.get().uri(this.base + "/students/{id}", id).retrieve().body(Student.class);
@@ -33 +33 @@ public class RemoteClient {
- return this.restTemplate.postForEntity(this.base + "/students", s, Student.class);
+ return this.restTemplate.post().uri(this.base + "/students").body(s).retrieve().toEntity(Student.class);
@@ -37 +37 @@ public class RemoteClient {
- this.restTemplate.delete(this.base + "/students/{id}", id);
+ this.restTemplate.delete().uri(this.base + "/students/{id}", id).retrieve().toBodilessEntity();
@@ -43,3 +43,2 @@ public class RemoteClient {
- return this.restTemplate.exchange(this.base + "/students", HttpMethod.GET, new HttpEntity<>(headers),
- new ParameterizedTypeReference<List<Student>>() {
- }).getBody();
+ return this.restTemplate.method(HttpMethod.GET).uri(this.base + "/students").headers(h -> h.addAll(new HttpEntity<>(headers).getHeaders())).body(new HttpEntity<>(headers).getBody()).retrieve().toEntity(new ParameterizedTypeReference<List<Student>>() {
+ }).getBody();
@@ -0,0 +1,5 @@
# 21-openrewrite-generated-client-run (Spring Boot 4.1.3, JDK 25)
find(1) -> Student[id=1, name=Asha]
create(Meera) -> 201 CREATED
remove(1) -> ok
all("tok") -> THROWS java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "body" is null
@@ -0,0 +1,5 @@
# 21-openrewrite-generated-client-run (Spring Boot 4.2.0, JDK 25)
find(1) -> Student[id=1, name=Asha]
create(Meera) -> 201 CREATED
remove(1) -> ok
all("tok") -> THROWS java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "body" is null
@@ -0,0 +1,2 @@
# 22-configured-client (Spring Boot 4.1.3, JDK 25)
RestClientConfig.apiClient GET /whoami -> server saw x-request-id: <36-character UUID>
@@ -0,0 +1,2 @@
# 22-configured-client (Spring Boot 4.2.0, JDK 25)
RestClientConfig.apiClient GET /whoami -> server saw x-request-id: <36-character UUID>
@@ -0,0 +1,13 @@
# 23-http-client-properties (spring-configuration-metadata.json, spring-boot-http-client jars)
Spring Boot 4.1.3
spring.http.clients.connect-timeout current
spring.http.clients.read-timeout current
spring.http.client.connect-timeout DEPRECATED -> spring.http.clients.connect-timeout
spring.http.client.read-timeout DEPRECATED -> spring.http.clients.read-timeout
Spring Boot 4.2.0
spring.http.clients.connect-timeout current
spring.http.clients.read-timeout current
spring.http.client.connect-timeout DEPRECATED -> spring.http.clients.connect-timeout
spring.http.client.read-timeout DEPRECATED -> spring.http.clients.read-timeout