Lots of existing (Spring) infrastructure accepts RestOperations
as a pluggable http/rest client and I could not find any adapter that implements the old RestOperations
, but is backed by the new RestClient
.
I know the RestClient
can be constructed from a RestTemplate
, but I would prefer - especially in a new/greenfield project - to base everything of the new RestClient
and have an adapter to map this back to RestOperations
when needed.
Are there plans for, or would you accept a PR for something along these lines ?
public class RestOperationsAdapter implements RestOperations {
public static RestOperationsAdapter create(RestClient restClient) {
....
}
....
}
Comment From: poutsma
The main reason for introducing RestOperations
interface was to serve as a thin layer around RestTemplate
, making it easier to mock HTTP interactions. Since RestClient
already is an interface, there is not similar need here.
Moreover, RestClient
uses the exact same underlying infrastructure as RestTemplate
does, so a RestClient
adapter for RestOperations
would only introduce an extra layer, whereas the remainder of the layers are the same.
Finally, in terms of API, RestTemplate
(and therefore RestOperations
) is an evolutionary dead-end. Any functionality we would introduce there would need additional overloaded methods, increasing the maintenance and usage complexity. The whole point of RestClient
was to offer a modern, fluent, and more flexible API than RestTemplate
ever can be. Retrofitting the former into the latter would ignore that.
Comment From: tgeens
My apologies for replying to a closed ticket, I think I did not articulate my motivation in the original post very well.
I do appreciate the modern, fluent and flexible API of RestClient
. I want to use it in my projects!!
But I can't, because there is an adoption problem:
Since
RestClient
already is an interface, there is not similar need here.
RestClient
is a fluent interface and doesn't look as easily mocked or stubbed, as RestOperations
. I don't expect infrastructure - for example DefaultClientCredentialsTokenResponseClient
- to switch out RestOperations
for RestClient
, as it makes it harder to test ?
That means that I'd better stick with RestTemplate
, because that is what I need to configure and inject in all my infrastructure. I could create a RestClient
from a RestTemplate
for newer things, but as long as something uses RestOperations
, I'll have to stick with RestTemplate
. It is the lowest common denominator after all.
Using an adapter approach, my baseline could be the new RestClient
, plus backwards-compatibility adapter for old infrastructure relying on RestOperations
.
Is there another strategy I'm not aware of, to acquire adoption for RestClient
?
My first iteration for reference: https://gist.github.com/tgeens/12b186ccffb0049b6eff8e33a80100dd