I think path() should be nullable in case you have a param that shouldn't always appear depending on the environment. in my case, I have an ingress prefix when I'm sending a request from my PC to a remote service but when it's in production, I don't need it and I would send the request directly since both services would be running in the same k8s cluster.
Comment From: sdeleuze
Even if the implementation of UriComponentsBuilder#path
could take a null parameter, I tend to think that's on purpose we require a non null value here, in order to ensure that when this is invoked, the path is set. Also this is consistent with other methods of the same familly (fromPath
, fromUri
, uri
and UriBuilder#path
).
Are you ok with this line of reasoning @rstoyanchev @bclozel?
Comment From: sdeleuze
@radSoftPer Could you please share a code sample of your use case to allow us to understand it correctly?
Comment From: radSoftPer
the names are changed a bit but the part where .path("/{servicePrefix}")
is the issue, when in production I don't need it but when I'm running it from IntelliJ i do.
String urlTemplate = UriComponentsBuilder.newInstance()
.scheme("http")
.host(vmVip)
.path("/{servicePrefix}")
.path("/isReady")
.buildAndExpand(servicePrefix)
.toUriString();
production(inside k8s cluster i use the service endpoint so i dont need to use a prefix):
http://serviceName-srv/isReady
development (need a prefix because i'm going through the ingress):
http://vmVip/servicePrefixName/isReady
Comment From: bclozel
I'm not sure I understand. In this case .path("/{servicePrefix}")
expands to "/"
(or an empty path) but not to null
. Why should we make this method argument @Nullable
then?
Comment From: sdeleuze
Yeah and for this use case, I would recommend to just use a UriComponentsBuilder
variable with a if
block that set conditionnaly the service prefix or something approaching. So I don't see a need here to make the parameter nullable.
Comment From: rstoyanchev
Given that path(String)
, unlike pathSegment(String)
, appends the value as-is without inserting any slashes, you can insert the complete path prefix:
String servicePrefix = ... ; // Either "/servicePrefixName" or "" (empty String)
String urlTemplate = UriComponentsBuilder.newInstance()
.scheme("http")
.host(vmVip)
.path("{servicePrefix}")
.pathSegment("isReady")
.buildAndExpand(servicePrefix)
.toUriString();
Comment From: radSoftPer
@rstoyanchev if i add an empty string to path() it will just ignore it? it wont add an unnecessary "/" ? like this: http://vmVip//isReady
if this creates a valid url even though the path()
has an empty string then thats a good solution for me, but even so i would have liked to see a function similar to queryParamIfPresent()
so it would be clearer.
does it make sense to have pathIfPresent()
?
Comment From: rstoyanchev
@radSoftPer, yes please check the Javadoc of path(String). It inserts into the path as-is, so you have full control there.
Comment From: radSoftPer
awesome.