Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring's UriComponentsBuilder.queryParam issue

Tags:

java

rest

spring

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("sysparm_limit", "2000000");
builder.queryParam("sysparm_offset", "0");
builder.queryParam("sysparm_exclude_reference_link", "true");
//this line is the issue because there is a = sign here
builder.queryParam("sysparm_query=user_name", snUser.getUser_name());
logger.info("URI after Query Param: " + builder.build().encode().toUri());

The output of this code is:

INFO: URI before Query Param: https://sandbox.service-now.com/api/now/v1/table/sys_user
INFO: URI after Query Param: https://sandbox.service-now.com/api/now/v1/table/sys_user?sysparm_limit=2000000&sysparm_offset=0&sysparm_exclude_reference_link=true&sysparm_query%3Duser_name=AX0011

The problem is with the final builder.queryParam. I am getting output as this:

sysparm_query%3Duser_name=AX0011

but what I want is:

sysparm_query=user_name=AX0011

So that eventually the final URI looks like this:

INFO: URI after Query Param: https://sandbox.service-now.com/api/now/v1/table/sys_user?sysparm_limit=2000000&sysparm_offset=0&sysparm_exclude_reference_link=true&sysparm_query=user_name=Z001NR6

So I tried replacing,

builder.queryParam("sysparm_query=user_name", snUser.getUser_name());

by:

builder.query("sysparm_query=user_name=" + snUser.getUser_name());

which changed the original output from:

INFO: URI after Query Param: https://sandbox.service-now.com/api/now/v1/table/sys_user?sysparm_limit=2000000&sysparm_offset=0&sysparm_exclude_reference_link=true&sysparm_query%3Duser_name=Z001NR6

to:

INFO: URI after Query Param: https://sandbox.service-now.com/api/now/v1/table/sys_user?sysparm_limit=2000000&sysparm_offset=0&sysparm_exclude_reference_link=true&sysparm_query=user_name%3DZ001NR6

Notice how sysparm_query%3Duser_name=Z001NR6 changed to sysparm_query=user_name%3DZ001NR6

Is ther anyway to see a = instead of %3D in the output?

like image 524
Mukul Tripathi Avatar asked Mar 13 '23 20:03

Mukul Tripathi


1 Answers

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
like image 173
fateddy Avatar answered Mar 27 '23 11:03

fateddy