BAEL-2924 Jersey request parameters (#7007)
* BAEL-2924 Jersey request parameters * BAEL-2924 Jersey request parameters
This commit is contained in:
parent
ef9f687f68
commit
fd5afd015a
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.jersey.server;
|
||||
|
||||
import javax.ws.rs.FormParam;
|
||||
import javax.ws.rs.HeaderParam;
|
||||
import javax.ws.rs.PathParam;
|
||||
|
||||
public class ItemParam {
|
||||
|
||||
@HeaderParam("headerParam")
|
||||
private String shopKey;
|
||||
|
||||
@PathParam("pathParam")
|
||||
private String itemId;
|
||||
|
||||
@FormParam("formParam")
|
||||
private String price;
|
||||
|
||||
public String getShopKey() {
|
||||
return shopKey;
|
||||
}
|
||||
|
||||
public void setShopKey(String shopKey) {
|
||||
this.shopKey = shopKey;
|
||||
}
|
||||
|
||||
public String getItemId() {
|
||||
return itemId;
|
||||
}
|
||||
|
||||
public void setItemId(String itemId) {
|
||||
this.itemId = itemId;
|
||||
}
|
||||
|
||||
public String getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(String price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ItemParam{shopKey='" + shopKey + ", itemId='" + itemId + ", price='" + price + '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.jersey.server;
|
||||
|
||||
import javax.ws.rs.*;
|
||||
|
||||
@Path("items")
|
||||
public class Items {
|
||||
|
||||
@GET
|
||||
@Path("/cookie")
|
||||
public String readCookieParam(@CookieParam("cookieParamToRead") String cookieParamToRead) {
|
||||
return "Cookie parameter value is [" + cookieParamToRead + "]";
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/header")
|
||||
public String readHeaderParam(@HeaderParam("headerParamToRead") String headerParamToRead) {
|
||||
return "Header parameter value is [" + headerParamToRead + "]";
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/path/{pathParamToRead}")
|
||||
public String readPathParam(@PathParam("pathParamToRead") String pathParamToRead) {
|
||||
return "Path parameter value is [" + pathParamToRead + "]";
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/query")
|
||||
public String readQueryParam(@QueryParam("queryParamToRead") String queryParamToRead) {
|
||||
return "Query parameter value is [" + queryParamToRead + "]";
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/form")
|
||||
public String readFormParam(@FormParam("formParamToRead") String formParamToRead) {
|
||||
return "Form parameter value is [" + formParamToRead + "]";
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/matrix")
|
||||
public String readMatrixParam(@MatrixParam("matrixParamToRead") String matrixParamToRead) {
|
||||
return "Matrix parameter value is [" + matrixParamToRead + "]";
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/bean/{pathParam}")
|
||||
public String readBeanParam(@BeanParam ItemParam itemParam) {
|
||||
return itemParam.toString();
|
||||
}
|
||||
}
|
|
@ -7,12 +7,13 @@ import java.util.logging.Logger;
|
|||
|
||||
import org.glassfish.grizzly.http.server.HttpServer;
|
||||
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
|
||||
import org.glassfish.jersey.server.ResourceConfig;
|
||||
|
||||
import com.baeldung.jersey.server.config.ViewApplicationConfig;
|
||||
|
||||
public class EmbeddedHttpServer {
|
||||
|
||||
private static final URI BASE_URI = URI.create("http://localhost:8082/");
|
||||
public static final URI BASE_URI = URI.create("http://localhost:8082/");
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
|
@ -32,4 +33,9 @@ public class EmbeddedHttpServer {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
public static HttpServer startServer() {
|
||||
final ResourceConfig rc = new ResourceConfig().packages("com.baeldung.jersey.server");
|
||||
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI.toString()), rc);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.jersey.server;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
import javax.ws.rs.client.WebTarget;
|
||||
|
||||
import org.glassfish.grizzly.http.server.HttpServer;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jersey.server.http.EmbeddedHttpServer;
|
||||
|
||||
public class ItemsUnitTest {
|
||||
|
||||
private HttpServer server;
|
||||
private WebTarget target;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
server = EmbeddedHttpServer.startServer();
|
||||
target = ClientBuilder.newClient().target(EmbeddedHttpServer.BASE_URI.toString());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCookieParameter_whenGet_thenReturnsExpectedText() {
|
||||
String paramValue = "1";
|
||||
String responseText = target.path("items/cookie").request().cookie("cookieParamToRead", paramValue).get(String.class);
|
||||
assertEquals("Cookie parameter value is [" + paramValue + "]", responseText);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHeaderParameter_whenGet_thenReturnsExpectedText() {
|
||||
String paramValue = "2";
|
||||
String responseText = target.path("items/header").request().header("headerParamToRead", paramValue).get(String.class);
|
||||
assertEquals("Header parameter value is [" + paramValue + "]", responseText);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPathParameter_whenGet_thenReturnsExpectedText() {
|
||||
String paramValue = "3";
|
||||
String responseText = target.path("items/path/" + paramValue).request().get(String.class);
|
||||
assertEquals("Path parameter value is [" + paramValue + "]", responseText);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue