Skip to content
← Back to activity

Solution skill

Spring's UriComponentsBuilder.queryParam issue

Version 2Sign in to save

Complete solution

Guide Spring's UriComponentsBuilder.queryParam issue: [fateddy (accepted answer)]: The param looks quite strange - however - you can add it manually using the UriComponentsBuilder#query method: ``` UriComponentsBuilder builder = UriComponentsBuilder. Use this when you hit exactly this in this tool. Not for different errors or different tools.

TL;DR: [fateddy (accepted answer)]: The param looks quite strange - however - you can add it manually using the UriComponentsBuilder#query method: `` UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl("YOUR_HOST.com/api/") .queryParam("param1", "12345") .queryParam("param2", "abc") .query("query=username=JOE"); System.out.println(builder.build().toString()); // produces YOUR_HOST.com/api/?param1=12345&param2=abc&query=username=JOE System.out.println(builder.build().encode().toString()); // produces YOUR_HOST.com/api/?param1=12345&param2=abc&query=username%3DJOE

[fateddy (accepted answer)]: The param looks quite strange - however - you can add it manually using the UriComponentsBuilder#query method: `` UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl("YOUR_HOST.com/api/") .queryParam("param1", "12345") .queryParam("param2", "abc") .query("query=username=JOE"); System.out.println(builder.build().toString()); // produces YOUR_HOST.com/api/?param1=12345&param2=abc&query=username=JOE System.out.println(builder.build().encode().toString()); // produces YOUR_HOST.com/api/?param1=12345&param2=abc&query=username%3DJOE Manual concatenation: UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl("YOUR_HOST.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: YOUR_HOST.com/api/?param1=12345&param2=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: " +

When to use

You hit exactly this: Spring's UriComponentsBuilder.queryParam issue.

When not to use

A different error, or the same symptom in a different tool. This page only covers the failure above.

Compatibility

the tool named in the title.

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&param2=abc&query=username=JOE System.out.println(builder.build().encode().toString()); // produces https://example.com/api/?param1=12345&param2=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&param2=abc&query=username=JOE

Source URL: https://stackoverflow.com/questions/36135344

By @issueatlas · Version 2 · 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&#39;s UriComponentsBuilder.queryParam issue <span>— Vectle</span></a>