4

Is it possible to build a url with multiple parameter values as a comma separated list?

The following snippet prints a url:

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;

public class MyMain {
    public static void main(String[] args) {

        MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
        queryParams.add("order", "1");
        queryParams.add("order", "2");

        System.out.println(UriComponentsBuilder.fromHttpUrl("http://example.com")
            .queryParams(queryParams)
            .build()
            .toString());
    }
}

The url produced by this code is:

http://example.com?order=1&order=2

What I would like to get is:

http://example.com?order=1,2

Using another framework is not an option and since I'm using a framework I would like to avoid building the logic to compose the url my way.

Is there anything in spring web or spring boot that can do this?

2 Answers 2

2

Try using String.join

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.Collection;
import static java.util.Arrays.asList;

public class MyMain {
    public static void main(String[] args) {

        Collection<String> values = asList("1","2");
        MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
        queryParams.add("order", String.join(",", values));

        System.out.println(UriComponentsBuilder.fromHttpUrl("http://example.com")
                .queryParams(queryParams)
                .build()
                .toString());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I had the same problem but I couldn't find any Spring solution either. Therefore I wrote a simple helper function for that:

@Test
public void testCreateValidMatrixVariableUrl() {
    MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
    queryParams.add("order", "1");
    queryParams.add("order", "2");

    String invalidUrl = UriComponentsBuilder.fromHttpUrl("http://example.com")
            .path("").query("order={order}")
            .buildAndExpand(queryParams)
            .toString();

    assertThat(prepareMatrixVariableUrl(invalidUrl)).isEqualTo("http://example.com?order=1,2");
}

private String prepareMatrixVariableUrl(String url) {
    return url.replaceAll("\\[", "")
            .replaceAll("\\]", "")
            .replaceAll(" ", "");
}

Keep in mind that prepareMatrixVariableUrl(...) replaces all brackets ([ and ]) and spaces () because without applying the helper function, your URL variable would have a format like: http://example.com?order=[1, 2]

Comments

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.