0

I need to pass the multiple value for a single query parameter in spring rest template , the query parameter is status and it values can be in progress,completed,rejected so I have pass them as values separated by comma , please advise is it the correct approach

HttpEntitiy<String> entity = httpEntitiyService.buildHttpEntity(null;"track status");
String url ="https://example/api/2/progress"

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
.querParam("status","inprogress,completed,rejected"); // please advise it is the correct approach 
1
  • It is entirely up to the server to decide what the status value is, so we can't help with that, since we don't know the server. Commented Mar 27, 2021 at 3:30

1 Answer 1

3

I would use the queryParams() method, which is specified as queryParams in the interface UriBuilder and defined as:

public UriComponentsBuilder queryParams(MultiValueMap<java.lang.String,java.lang.String> params)

In your case, you can do:

MultiValueMap<String, String> myParams = new LinkedMultiValueMap<String, String>();
myParams.add("status", "inprogress");
myParams.add("status", "completed");
myParams.add("status", "rejected");

And implement it:

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
        .queryParams(myParams)
        .build()
        .toUriString();

You can also implement in a similar manner, except we just use the MultiValueMap:

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
        .path("")
        .query("status={status}")
        .buildAndExpand(myParams)
        .toString();
Sign up to request clarification or add additional context in comments.

2 Comments

what about if I want to add one more query parameter that if of integer type let say page_size having value of 200 integer type
@asderfgh It wouldn't matter. UriComponentsBuilder automatically encodes the parameters for you, so you could just send it as a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.