I'm using Spring Framework 5.3.4 with Spring Boot 2.4.3.

I have a controller declared in this way:

@Controller
@RequestMapping("${saml.sp.controller-base-mapping}")
public class ServiceProviderController {
  /...
}

So, the property saml.sp.controller-base-mapping is externalized in application.properties file: if its value is sp, this should be equivalent to: @RequestMapping("sp"). And indeed Spring Web MVC handles this well.

However, if I then use MvcUriComponentsBuilder to retrieve the corresponding URL:

MvcUriComponentsBuilder.fromController(ServiceProviderController.class).build().toURL();

This returns a UriComponents of: http://localhost:8081/${saml.sp.controller-base-mapping}

First problem: I would have expected the externalized property to be already resolved (so to get http://localhost:8081/sp), because this should be an expansion mechanism that comes BEFORE path variable expansion.

Indeed (and here comes the second problem), if I try to do:

MvcUriComponentsBuilder.fromController(ServiceProviderController.class).buildAndExpand("sp");

I then get a UriComponents of: http://localhost:8081/$sp

This is somewhat expected, since path variable expansion is another story and does not use the "$" marker.

Comment From: rstoyanchev

MvcUriComponentsBuilder looks up the annotation values and so it needs to replace variables, but currently it doesn't do that and we would need to make a change to support it. In the mean time, as a workaround you can have a StringValueResolver injected via EmbeddedValueResolverAware and then apply it like this:

String s = MvcUriComponentsBuilder.fromController(MyController.class).build().toUriString();
s = valueResolver.resolveStringValue(s);
UriComponents uriComponents = UriComponentsBuilder.fromUriString(s)...

Comment From: mauromol

In the mean time, as a workaround you can have a StringValueResolver injected via EmbeddedValueResolverAware and then apply it like this:

Thank you for the suggested workaround!

Comment From: lower-case

Hi @rstoyanchev I'd like to work on this. I'm going through the code, will raise a PR soon.

Comment From: lower-case

Have raised this Draft PR. Please review the approach and provide your feedback. Working on test cases. Thanks!