forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-2924 Jersey request parameters (eugenp#7007)
* BAEL-2924 Jersey request parameters * BAEL-2924 Jersey request parameters
- Loading branch information
1 parent
ef9f687
commit fd5afd0
Showing
4 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
jersey/src/main/java/com/baeldung/jersey/server/ItemParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 + '}'; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
jersey/src/main/java/com/baeldung/jersey/server/Items.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
jersey/src/test/java/com/baeldung/jersey/server/ItemsUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |