Immutable solution version
Spring's UriComponentsBuilder.queryParam issue
Complete solution
The param looks quite strange - however - you can add it manually using the UriComponentsBuilder#query method: ``` UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl("https://example.com/api/") .queryParam("param1", "12345") .queryParam("param2", "abc") .query("query=username=JOE"); System.out.println(builder.build().toString()); // produces https://example.com/api/?param1=12345¶m2=abc&query=username=JOE System.out.println(builder.build().encode().toString()); // produces ht...
[fateddy (accepted answer)]: The param looks quite strange - however - you can add it manually using the UriComponentsBuilder#query method: `` UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl("https://example.com/api/") .queryParam("param1", "12345") .queryParam("param2", "abc") .query("query=username=JOE"); System.out.println(builder.build().toString()); // produces https://example.com/api/?param1=12345¶m2=abc&query=username=JOE System.out.println(builder.build().encode().toString()); // produces https://example.com/api/?param1=12345¶m2=abc&query=username%3DJOE Manual concatenation: UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl("https://example.com/api/") .queryParam("param1", "12345") .queryParam("param2", "abc"); // the parameter has to be properly url-encoded manually (not shown here) String uri = builder.build().encode().toString() + "&query=username=JOE"; System.out.println(uri); // produces: https://example.com/api/?param1=12345¶m2=abc&query=username=JOE
Context: Stack Overflow #36135344 (accepted answer, 10 votes, 2 answers): I have recently switched to Spring for consuming REST API calls hosted by ServiceNow. I am building my URI as below: ``` UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl.toString()); logger.info("URI before Query Param: " + builder.build().encode().toUri()); builder.queryParam("sysparmlimit", "2000000"); builder.queryParam("sysparmoffset", "0"); builder.queryParam("sysparmexcludereferencelink", "true"); //this line is the issue because there is a = sign here builder.queryParam("sysparmquery=username", snUser.getUsername()); logger.info("URI after Query Param: " +
Evidence
Source: Stack Overflow #36135344 (accepted answer, 10 votes, 2 answers): I have recently switched to Spring for consuming REST API calls hosted by ServiceNow. I am building my URI as below: ``` UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl.toString()); logger.info("URI before Query Param: " + builder.build().encode().toUri()); builder.queryParam("sysparmlimit", "2000000"); builder.queryParam("sysparmoffset", "0"); builder.queryParam("sysparmexcludereferencelink", "true"); //this line is the issue because there is a = sign here builder.queryParam("sysparmquery=username", snUser.getUsername()); logger.info("URI after Query Param: " +
Verified resolution: [fateddy (accepted answer)]: The param looks quite strange - however - you can add it manually using the UriComponentsBuilder#query method: `` UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl("https://example.com/api/") .queryParam("param1", "12345") .queryParam("param2", "abc") .query("query=username=JOE"); System.out.println(builder.build().toString()); // produces https://example.com/api/?param1=12345¶m2=abc&query=username=JOE System.out.println(builder.build().encode().toString()); // produces https://example.com/api/?param1=12345¶m2=abc&query=username%3DJOE Manual concatenation: UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl("https://example.com/api/") .queryParam("param1", "12345") .queryParam("param2", "abc"); // the parameter has to be properly url-encoded manually (not shown here) String uri = builder.build().encode().toString() + "&query=username=JOE"; System.out.println(uri); // produces: https://example.com/api/?param1=12345¶m2=abc&query=username=JOE
Source URL: https://stackoverflow.com/questions/36135344
By @issueatlas · Version 1 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_utEbgQGP1CyuuYFENXVjUg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Spring's UriComponentsBuilder.queryParam issue <span>— Vectle</span></a>