-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Add static factory methods to RequestCallback and ResponseExtractor [SPR-8604] #13247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Shai Yallin commented Same here. I was trying to overcome the issue in #12671 by subclassing HttpEntityRequestCallback and ended up having to actually duplicate HttpEntityRequestCallback, AcceptHeaderRequestCallback and ResponseEntityResponseExtractor. |
Rossen Stoyanchev commented Craig, the AcceptHeaderRequestCallback accomplishes a very specific task that is not customizable (other than the responseType), it is internal to the RestTemplate and is already used in the places where it's needed. Could elaborate on your reasons for this request? |
Rossen Stoyanchev commented Craig, Shai Yallin, as mentioned above, these callback classes perform tasks internal to the RestTemplate and are not open for extension. Providing a specific example of what you're trying to customize or do would help. |
Mauro Molinari commented Could you please reconsider this? My use case is this: I want to supply a Right now, |
Rossen Stoyanchev commented Arjen Poutsma what do you think about exposing static factory methods on RequestCallback and ResponseExtractor? Those would invoke the protected methods on RestTemplate -- acceptHeaderRequestCallback, httpEntityCallback, httpEntityCallback, and responseEntityExtractor respectively. That would allow overriding only the RequestCallback or ResponseExtractor but not both. |
Arjen Poutsma commented Sounds good to me. |
Rossen Stoyanchev commented Static factory methods can't work actually, since the inner classes are not static themselves and require a surrounding instance of RestTemplate. Changing the methods I propose to make the inner classes public. I see no strong reason to not expose them, and that doesn't have any of the downsides of the other options. Those implementations seem to be of interest, and there are legitimate reasons to use them, e.g. to provide only So the following would become possible: RequestCallback callback =
template.new AcceptHeaderRequestCallback(String.class);
RequestCallback callback =
template.new HttpEntityRequestCallback("body", String.class);
ResponseExtractor<ResponseEntity<String>> extractor =
template.new ResponseEntityResponseExtractor<>(String.class);
ResponseExtractor<HttpHeaders> extractor =
new RestTemplate.HeadersExtractor(); |
Arjen Poutsma commented I see two problems with making these classes public as is. Firstly, these inner classes have been designed for private use only. If we would make them public, we would need to refactor them for extensibility: the larger methods need to split into smaller ones with overridable template methods, proper input sanitation, etc. It's not as simple as changing Secondly, and this is more an aesthetic issue: I really do not care for public non-static inner classes. Instantiating them with Personally, I would actually prefer to use public methods to expose them, returning public interfaces like
I do see the concern for adding even visible methods to the |
Rossen Stoyanchev commented Alright, I guess the intent is to provide some way to access those instances, and not to for extension purposes. I'll go with making those methods public. That's close to the static factory methods idea but clearly they cannot be static. |
Craig opened SPR-8604 and commented
I'm attempting to use RestTemplate.execute() and it takes a RequestCallback instance and a ResponseExtractor instance as parameters. It seems that I should be able to use (via direct instantiation or inheritance) the really useful RestTemplate implementations of these interfaces, but they're all marked private. Can they please be marked public?
Affects: 5.0.4
Referenced from: commits 79e809b
3 votes, 6 watchers
The text was updated successfully, but these errors were encountered: