[BAEL-7069] Pass list as a query parameter in Jersey (#15051)
* Pass list as a query parameter in Jersey * Address review comment * Address review comment --------- Co-authored-by: rajatgarg <rajatgarg@adobe.com>
This commit is contained in:
parent
63953b74f3
commit
da8167cf17
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.jersey.client.listdemo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.QueryParam;
|
||||
|
||||
@Path("/")
|
||||
public class JerseyListDemo {
|
||||
@GET
|
||||
public String getItems(@QueryParam("items") List<String> items) {
|
||||
return "Received items: " + items;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.jersey.client.listdemo;
|
||||
|
||||
import org.glassfish.jersey.server.ResourceConfig;
|
||||
|
||||
public class ListDemoApp extends ResourceConfig {
|
||||
public ListDemoApp() {
|
||||
packages("com.baeldung.jersey.client.listdemo");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.jersey.client.listdemo;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.glassfish.jersey.test.JerseyTest;
|
||||
import org.glassfish.jersey.test.TestProperties;
|
||||
import org.junit.Test;
|
||||
|
||||
import jakarta.ws.rs.core.Application;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.UriBuilder;
|
||||
|
||||
public class JerseyListDemoUnitTest extends JerseyTest {
|
||||
|
||||
@Override
|
||||
protected Application configure() {
|
||||
enable(TestProperties.LOG_TRAFFIC);
|
||||
enable(TestProperties.DUMP_ENTITY);
|
||||
return new ListDemoApp();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenUsingQueryParam_thenPassParamsAsList() {
|
||||
Response response = target("/")
|
||||
.queryParam("items", "item1", "item2")
|
||||
.request()
|
||||
.get();
|
||||
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
|
||||
assertEquals("Received items: [item1, item2]", response.readEntity(String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenUsingCommaSeparatedString_thenPassParamsAsList() {
|
||||
Response response = target("/")
|
||||
.queryParam("items", "item1,item2")
|
||||
.request()
|
||||
.get();
|
||||
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
|
||||
assertEquals("Received items: [item1,item2]", response.readEntity(String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenUsingUriBuilder_thenPassParamsAsList() {
|
||||
List<String> itemsList = Arrays.asList("item1", "item2");
|
||||
UriBuilder builder = UriBuilder.fromUri("/");
|
||||
for (String item : itemsList) {
|
||||
builder.queryParam("items", item);
|
||||
}
|
||||
URI uri = builder.build();
|
||||
String expectedUri = "/?items=item1&items=item2";
|
||||
assertEquals(expectedUri, uri.toString());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue