Migrate RestTemplate to RestClient

Proprietary

Migrates 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.MigrateRestTemplateToRestClient
mod run . --recipe=io.moderne.java.spring.boot3.MigrateRestTemplateToRestClient

Install 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.

  1. Migrate RestTemplateBuilder to RestClient.Builder
  2. Migrate RestTemplateCustomizer to RestClientCustomizer
  3. Migrate RestTemplate constructor calls to RestClient
  4. Change type RestTemplate to RestClient
  5. Migrate RestTemplate API calls to RestClient
  6. Migrate RestTemplate configuration to RestClient
  7. Simplify a call chain
  8. Add comment to method invocations
  9. Add comment to method invocations
  10. Add comment to method invocations
  11. Add comment to method invocations
  12. Add comment to method invocations
  13. Add comment to method invocations
  14. Add comment to method invocations
  15. Add comment to method invocations
  16. Add comment to method invocations
  17. Add comment to method invocations
  18. Add comment to method invocations
  19. Add comment to method invocations
  20. Add comment to method invocations