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 UriComponentsBuilder.clone() [SPR-12494] #17100

Closed
spring-projects-issues opened this issue Dec 2, 2014 · 4 comments
Closed

Add UriComponentsBuilder.clone() [SPR-12494] #17100

spring-projects-issues opened this issue Dec 2, 2014 · 4 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Hendy Irawan opened SPR-12494 and commented

In Spring MVC it's possible to inject UriComponentsBuilder into a @RestController method, making building links convenient.

However, when crafting a hypermedia response, there needs to be many links, some of them may refer to different places.

Calling .replacePath() (or any method) will mutate the UriComponentsBuilder, rendering it somewhat useless for next invocations. Also, it's impossible to start "clean" (from state of injection).

Please add clone() so it's easy for this use case.


Affects: 4.1.2

Referenced from: commits 189ec75

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

UriComponentsBuilder injectionion is a convenience that's relatively easy to forgo. I'm wondering if simply calling ServletUriComponentsBuilder would work alright for you?

If I understand this what you'd like:

@RequestMapping
public void handle(UriComponentsBuilder originalBuilder) {

	UriComponentsBuilder builder = originalBuilder.clone();
	// use builder...

	builder = originalBuilder.clone();
	// user builder...

}

which can be done as follows:

@RequestMapping
public void handle() {

	UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentServletMapping();
	// use builder

	builder = ServletUriComponentsBuilder.fromCurrentServletMapping();
	// user builder
}

It's bit longer but a private helper method (perhaps even in a base class) should help:

@RequestMapping
public void handle() {

	UriComponentsBuilder builder = createUriComponentsBuilder();
	// use builder

	builder = createUriComponentsBuilder();
	// user builder
}

// In some base class...		

protected UriComponentsBuilder createUriComponentsBuilder() {
	return ServletUriComponentsBuilder.fromCurrentServletMapping();
}

@spring-projects-issues
Copy link
Collaborator Author

Hendy Irawan commented

Thanks Rossen! Now I know where it comes from... :)

even createUriComponentsBuilder() is still longer than b.clone(), but at least now I have an alternative.

b.clone() feels cleaner to me :) and also, it will work for various other purposes, not just this one

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Alright, I've added clone() to UriComponentsBuilder hierarchy.

@spring-projects-issues
Copy link
Collaborator Author

Hendy Irawan commented

Thank you! :)

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 4.1.3 milestone Jan 11, 2019
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) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants