Hello.
When using UriComponentsBuilder, an exception occurs in the following code: - jdoodle link : https://jdoodle.com/a/1XG2 - code
import org.springframework.web.util.UriComponentsBuilder;
public class Test {
public static void main(String[] args) {
try {
String uri = "http://test.com?param1={aaa}¶m2={bbb}¶m3={ccc}";
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uri);
String param1 = "1";
String param2 = "2";
String param3 = "{3}";
String uriString = uriComponentsBuilder.buildAndExpand(param1, param2, param3).toUriString();
System.out.println("test1 : " + uriString);
} catch (Exception e) {
e.printStackTrace();
}
try {
String uri = "http://test.com?param1={aaa}¶m2={bbb}¶m3={ccc}";
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uri);
String param1 = "1";
String param2 = "2";
String param3 = "{3}";
UriComponentsBuilder uriComponentsBuilder1 = uriComponentsBuilder.queryParam("param1", param1);
String uriString = uriComponentsBuilder1.buildAndExpand(param2, param3).toUriString();
System.out.println("test2 : " + uriString);
} catch (Exception e) {
e.printStackTrace();
}
}
}
- output
test1 : http://test.com?param1=1¶m2=2¶m3={3}
java.lang.IllegalArgumentException: Not enough variable values available to expand 'ccc'
at org.springframework.web.util.UriComponents$VarArgsTemplateVariables.getValue(UriComponents.java:367)
at org.springframework.web.util.HierarchicalUriComponents$QueryUriTemplateVariables.getValue(HierarchicalUriComponents.java:1053)
at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:262)
at org.springframework.web.util.HierarchicalUriComponents.lambda$expandQueryParams$5(HierarchicalUriComponents.java:445)
at java.util.Map.forEach(Map.java:630)
at org.springframework.web.util.HierarchicalUriComponents.expandQueryParams(HierarchicalUriComponents.java:441)
at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:430)
at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:51)
at org.springframework.web.util.UriComponents.expand(UriComponents.java:172)
at org.springframework.web.util.UriComponentsBuilder.buildAndExpand(UriComponentsBuilder.java:433)
at Test.main(Test.java:31)
Is it wrong to use it? Or is it a known issue? If it's a bug, can I apply for a pullrequest for that content?
Comment From: sbrannen
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
Although I have stated our policy above, I can provide you an answer here.
To replace a query parameter, you need to invoke replaceQueryParam() instead of queryParam(). As the Javadoc states, the latter appends an additional query parameter.
Whereas, if you use replaceQueryParam() as in the following modified example, the output will be:
test1 : http://test.com?param1=1¶m2=2¶m3={3}
test2 : http://test.com?param2=2¶m3={3}¶m1=1
UriComponentsBuilder uriComponentsBuilder1 = uriComponentsBuilder.replaceQueryParam("param1", param1);
String uriString = uriComponentsBuilder1.buildAndExpand(param2, param3).toUriString();
System.out.println("test2 : " + uriString);