Skip to content
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

Expand relative URI for RestTemplate if possible #27188

Closed
wants to merge 1 commit into from

Conversation

quaff
Copy link
Contributor

@quaff quaff commented Jul 19, 2021

RestTemplate template = new RestTemplate();
template.setUriTemplateHandler(new DefaultUriBuilderFactory("http://localhost:8080"));
template.getForObject("/users/{id}", String.class, 1);
template.getForObject("/users/1", String.class);
// following will works as above after this commit
template.getForObject(URI.create("/users/1"), String.class);

template.exchange(RequestEntity.method(HttpMethod.GET, "/users/{id}", 1).build(), String.class);
template.exchange(RequestEntity.method(HttpMethod.GET, "/users/1").build(), String.class);
// following will works as above after this commit
template.exchange(RequestEntity.method(HttpMethod.GET, URI.create("/users/1")).build(), String.class);

It will make Spring Boot TestRestTemplate much more cleaner, save it from ubiquitous applyRootUriIfNecessary
https://github.com/spring-projects/spring-boot/blob/05a64ecb2ca237d963866665d3a0e7fd1920fee6/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java#L942-L948

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 19, 2021
@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Nov 10, 2021
It will make Spring Boot TestRestTemplate much more cleaner, save it from ubiquitous applyRootUriIfNecessary
@quaff
Copy link
Contributor Author

quaff commented Jun 20, 2023

RestTemplate is still relevant, could you review this PR? @rstoyanchev

@quaff quaff mentioned this pull request Jul 5, 2023
@snicoll snicoll requested a review from rstoyanchev August 26, 2023 15:13
@bclozel bclozel self-requested a review September 15, 2023 13:22
Copy link
Contributor

@rstoyanchev rstoyanchev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will result in double encoding if the URI has encoded characters, e.g. URI.create("/users/1%202") results in http://localhost:8080/users/1%25202.

Generally with the method variants that take URI, we assume the URI has been fully initialized externally and we don't touch it further. That way if the URI template doesn't work for you for some reason, you can always take over.

I'm not sure why you want to pass a URI for a relative URL, or how you prepare it, but you could use the same UriBuilderFactory to that instead of URI.create.

@rstoyanchev rstoyanchev added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants