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

Add String based URI template variants to RequestEntity [SPR-17551] #22083

Open
spring-issuemaster opened this issue Nov 29, 2018 · 4 comments

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Nov 29, 2018

Ryon Day opened SPR-17551 and commented

The RestTemplateBuilder documentation for the rootUri() method states:

"Set a root URL that should be applied to each request that starts with '/'."

However, the following code does not result in RestTemplate expanding the URI:

UriTemplate template = new UriTemplate("/foo?bar={bar}");

this.restTemplate = restTemplateBuilder
 .rootUri(URI.create("http://foo.bar"))
 .build();

URI uri = template.expand("baz");
RequestEntity<Void> req = RequestEntity.get(uri)
 .accept(MediaType.APPLICATION_JSON)
 .header(accountLinkHeader, accountLinksHeader)
 .build();
String response = restTemplate.exchange(req, String.class);

 

Moreover, the exception received is EXTREMELY ambiguous when this doesn't work. In fact, the Spring exception thrown occludes the initial Apache exception (which states there's no host).


Affects: 5.1.3

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Nov 30, 2018

Rossen Stoyanchev commented

RestTemplateBuilder is a Spring Boot class, so we can't change anything here. As far as the underlying mechanism, it's using the uriTemplateHandler which is documented as "Configure a strategy for expanding URI templates". In other words only the methods that accept a String URI template to be expanded have this handler applied. All the methods that accept a java.net.URI, the URI is considered fully built and used as is. So you might want to create a ticket in Boot to make the Javadoc on rootUri more clear about that and/or to link to the JavaDoc of the underlying mechanism it's using.

I think what could be a reasonable request here is to provide a way to supply a URI template via RequestEntity so that you don't have to expand it externally, and lose the advantage of the applying a baseUrl.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Nov 30, 2018

Rossen Stoyanchev commented

Resolving for now but feel free to comment or reopen if needed.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Nov 30, 2018

Ryon Day commented

Thanks for the quick attention to this and the explanation. It does indeed feel like this is a mismatch between RestTemplateBuilder and the actual RestTemplate

Supplying a UriTemplate in RequestEntity would be super nice here; it would let us use this very nice pattern:

UriTemplate template = new UriTemplate("/foo?bar={bar}");

this.restTemplate = restTemplateBuilder
 .rootUri(URI.create("http://foo.bar"))
 .build();

URI uri = template.expand("baz");
RequestEntity<Void> req = RequestEntity.get(uri)
 .accept(MediaType.APPLICATION_JSON)
 .header(accountLinkHeader, accountLinksHeader)
 .build();
String response = restTemplate.exchange(req, String.class); 

 

which is simply so, so much nicer and clearer than this pattern:

String template = "/foo?bar={bar}";

HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
headers.set("Foo", "Bar");

HttpEntity entity = new HttpEntity(headers);

ResponseEntity<String> resp = restTemplate.exchange(template,
    HttpMethod.GET,
    entity,
    String.class,
    baz);

(Not being able to chain .set (or any of the other methods) when using HttpHeaders causes me almost physical pain :D)

 

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Nov 30, 2018

Rossen Stoyanchev commented

Okay I'm re-purposing the ticket to be add String-based URI template method variants to RequestEntity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.