Migrate RestTemplate to RestClient
ProprietaryMigrates Spring's RestTemplate to the modern RestClient API introduced in Spring Framework 6.1. RestClient provides a fluent, synchronous API that is the recommended approach for new development. This recipe converts constructor calls, type declarations, and common method invocations (getForObject, getForEntity, postForObject, postForEntity, patchForObject, put, delete, headForHeaders, postForLocation, optionsForAllow, exchange) to their RestClient equivalents, and then folds setter-based bean configuration (setErrorHandler, setMessageConverters, setRequestFactory, ...) into the immutable RestClient.Builder API where it is safe to do so. The configuration fold runs after the type change, so it detects setters on the already-retyped RestClient receiver. Spring Boot's RestTemplateBuilder and RestTemplateCustomizer are migrated to the auto-configured RestClient.Builder and RestClientCustomizer. A repository is left on RestTemplate when the retyped code would not compile: when it extends RestTemplate or RestTemplateBuilder, refers to RestOperations, passes customizers to new RestTemplateBuilder(..), calls a builder method without a direct RestClient.Builder equivalent, or exchanges a RestTemplate, RestTemplateBuilder or RestTemplateCustomizer with code it does not declare. It is also left on RestTemplate when it calls exchange with a RequestEntity other than a variable whose body type is Void. So is a repository that sends an HttpEntity whose body is only known at runtime, as RestClient fails on a null body.
io. moderne. java. spring. boot3. MigrateRestTemplateToRestClientmod run . --recipe= io. moderne. java. spring. boot3. MigrateRestTemplateToRestClientInstall it once from Recipe actions, then run it on every repository under the current directory. New to the Moderne CLI? Set it up.
Recipe list
It runs these recipes, in this order.
- Migrate
RestTemplateBuildertoRestClient.Builder - Migrate
RestTemplateCustomizertoRestClientCustomizer - Migrate
RestTemplateconstructor calls toRestClient - Change type
RestTemplatetoRestClient - Migrate
RestTemplateAPI calls toRestClient - Migrate
RestTemplateconfiguration toRestClient - Simplify a call chain
- Add comment to method invocations
- Add comment to method invocations
- Add comment to method invocations
- Add comment to method invocations
- Add comment to method invocations
- Add comment to method invocations
- Add comment to method invocations
- Add comment to method invocations
- Add comment to method invocations
- Add comment to method invocations
- Add comment to method invocations
- Add comment to method invocations
- Add comment to method invocations