[RestTemplate Java] Use UriTemplate instead of URI object #1184
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem statement
When using BOAT to generate Rest Template in Java it generates ApiClient class which uses URI object when creating RequestEntity which is then used in rest template itself. The problem is that RestTemplate when URI object is used has access to ONLY full URL of the request with all url/query params already filled out with exact values.
This creates a problem for observability and monitoring using OOTB metrics for rest template from spring which is http.client.requests. How it works in spring-boot 3 is that they are taking the URI TEMPLATE and putting that into uri tag for http.client.requests metric. Full URL is not used in that metric due to high cardinality.
URI Template is available only using specific approaches while using RestTemplate, so not all constructors/method will use that. The current code uses constructor with URI object which does not provide any uri template but a final URL only and we ends up getting the value "none" in uri tag in the metrics all the time (because thats the default value if there is no uri template).
Solution
The solution to a problem is the code added. It is not passing URI object to the RequestEntity and instead it uses a constructor with uri template and a list of variables which is then populated to http.client.requests metric which properly construct uri tag with URI template. The final URI is actually constructed inside a rest template class using the UriTemplateHandler class as a helper method, so to the the final "doExecute" method in RestTemplate we are passing both - final uri with pre-filled variables and the uri template.
The code was tested locally and actually comes from newest version of open api generator templates for Rest Template.