Skip to content

ControllerLinkBuilderFactory should resolve placeholders #220

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

Closed
51f0x opened this issue Jul 25, 2014 · 6 comments
Closed

ControllerLinkBuilderFactory should resolve placeholders #220

51f0x opened this issue Jul 25, 2014 · 6 comments

Comments

@51f0x
Copy link

51f0x commented Jul 25, 2014

The ControllerLinkBuilderFactory does not resolve placeholders in RequestMapping annotations.

Ex. @RequestMapping(value = "${myPath}/users")

The handling should be the same as in the RequestMappingHandlerMapping.

Stacktrace:

java.lang.IllegalArgumentException: Map has no value for 'myPath'
at org.springframework.web.util.UriComponents$MapTemplateVariables.getValue(UriComponents.java:277) ~[spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:221) ~[spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.web.util.HierarchicalUriComponents$FullPathComponent.expand(HierarchicalUriComponents.java:642) ~[spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:311) ~[spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:46) ~[spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.web.util.UriComponents.expand(UriComponents.java:152) ~[spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.web.util.UriComponentsBuilder.buildAndExpand(UriComponentsBuilder.java:288) ~[spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.hateoas.mvc.ControllerLinkBuilderFactory.linkTo(ControllerLinkBuilderFactory.java:145) ~[spring-hateoas-0.15.0.RELEASE.jar:na]
at org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo(ControllerLinkBuilder.java:135) ~[spring-hateoas-0.15.0.RELEASE.jar:na]

@dschulten
Copy link
Contributor

Normally, when linking to methods with request mappings having path variables, the variable value should be defined by some method argument. You pass it in after methodOn() when you 'call' the target method.

Could you please post how you use linkTo(methodOn())?

Btw. the notation with $ is not what I'm used to. Did you mean "/{myPath}/users"?

Dietrich Schulten
Escalon System-Entwicklung
D-74199 Donnbronn

---- Matthias schrieb ----

The ControllerLinkBuilderFactory does not resolve placeholders in RequestMapping annotations.

Ex. @RequestMapping(value = "${myPath}/users")

The handling should be the same as in the RequestMappingHandlerMapping.

Stacktrace:

java.lang.IllegalArgumentException: Map has no value for 'myPath'
at org.springframework.web.util.UriComponents$MapTemplateVariables.getValue(UriComponents.java:277) ~[spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:221) ~[spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.web.util.HierarchicalUriComponents$FullPathComponent.expand(HierarchicalUriComponents.java:642) ~[spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:311) ~[spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:46) ~[spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.web.util.UriComponents.expand(UriComponents.java:152) ~[spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.web.util.UriComponentsBuilder.buildAndExpand(UriComponentsBuilder.java:288) ~[spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.hateoas.mvc.ControllerLinkBuilderFactory.linkTo(ControllerLinkBuilderFactory.java:145) ~[spring-hateoas-0.15.0.RELEASE.jar:na]
at org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo(ControllerLinkBuilder.java:135) ~[spring-hateoas-0.15.0.RELEASE.jar:na]


Reply to this email directly or view it on GitHub.

@51f0x
Copy link
Author

51f0x commented Aug 7, 2014

Spring MVC does resolve the @RequestMapping(value = "${myPath}/users") to a mapping like "/somePath/users" if the PropertyPlaceholderConfigurer resolves ${myPath} to "somePath". ${myPath} is therefor replaced when starting the application. All arguements like you mention {xxx} (PathVariables) must be given in the request.
I would like to have the same behaviour in the ControllerLinkBuilderFactory as in the RequestMappingHandlerMapping of Spring MVC.

I'm using the ControllerLinkBuilder like: linkTo(methodOn(RestUsersController.class).getAllUsers()).withSelfRel())

@sp00m
Copy link

sp00m commented Sep 21, 2015

A solution could be to make AnnotationMappingDiscoverer.getMapping(Class<?>, Method) call applicationContext.getBeanFactory().resolveEmbeddedValue(...) (or use an EmbeddedValueResolverAware instance) before returning the discovered mapping, what do you think about it?

@51f0x
Copy link
Author

51f0x commented Sep 21, 2015

Sure, this would be a good solution.

@sp00m
Copy link

sp00m commented Sep 22, 2015

I haven't been able to implement the fix in spring-hateoas directly, since AnnotationMappingDiscoverer isn't a Spring managed bean, and I can't find a way to access an EmbeddedValueResolverAware instance then... I hope someone who masters Spring better than me will be able to handle it ;)

I implemented a temporary fix in my projects by extending ControllerLinkBuilderFactory, see LinkBuilder and LinkBuilderFactory.

@gregturn
Copy link
Contributor

Resolved via #361.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants