Skip to content

Commit c2d4ae2

Browse files
committed
Merge pull request #223 from xcoulon/jaxrs-beanparam
Adding JAX-RS @BeanParam example
2 parents 36ec58a + 2545e94 commit c2d4ae2

File tree

8 files changed

+172
-0
lines changed

8 files changed

+172
-0
lines changed

jaxrs/beanparam/README.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
= JAX-RS BeanParam
2+
3+
This example demonstrate the use of a +@BeanParam+ annotation to group some of the request parameters in a user bean, in order to avoid having too many paramaters in the method signature.
4+
5+
The user type annotated with +@BeanParam+ may contain fields or setter methods annotated with +@MatrixParam+, +@QueryParam+, +@PathParam+, +@CookieParam+ or +@HeaderParam+.

jaxrs/beanparam/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.jaxrs</groupId>
6+
<artifactId>jaxrs-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.javaee7.jaxrs</groupId>
12+
<artifactId>beanparam</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<packaging>war</packaging>
15+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.javaee7.jaxrs.beanparam;
2+
3+
import javax.ws.rs.ApplicationPath;
4+
import javax.ws.rs.core.Application;
5+
6+
/**
7+
* @author Arun Gupta
8+
*/
9+
@ApplicationPath("webresources")
10+
public class MyApplication extends Application {
11+
12+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.javaee7.jaxrs.beanparam;
2+
3+
import javax.ws.rs.PathParam;
4+
5+
/**
6+
* @author xcoulon
7+
*
8+
*/
9+
public class MyPathParams {
10+
11+
@PathParam("id1")
12+
private String id1;
13+
14+
private String id2;
15+
16+
public String getId1() {
17+
return id1;
18+
}
19+
20+
public void setId1(String id1) {
21+
this.id1 = id1;
22+
}
23+
24+
public String getId2() {
25+
return id2;
26+
}
27+
28+
@PathParam("id2")
29+
public void setId2(String id2) {
30+
this.id2 = id2;
31+
}
32+
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.javaee7.jaxrs.beanparam;
2+
3+
import javax.ws.rs.QueryParam;
4+
5+
/**
6+
* @author xcoulon
7+
*
8+
*/
9+
public class MyQueryParams {
10+
11+
@QueryParam("param1")
12+
private String param1;
13+
14+
@QueryParam("param2")
15+
private String param2;
16+
17+
@QueryParam("param3")
18+
private String param3;
19+
20+
public String getParam1() {
21+
return param1;
22+
}
23+
24+
public String getParam2() {
25+
return param2;
26+
}
27+
28+
public String getParam3() {
29+
return param3;
30+
}
31+
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.javaee7.jaxrs.beanparam;
2+
3+
import javax.ws.rs.BeanParam;
4+
import javax.ws.rs.GET;
5+
import javax.ws.rs.Path;
6+
import javax.ws.rs.Produces;
7+
import javax.ws.rs.core.MediaType;
8+
9+
/**
10+
* @author Xavier Coulon
11+
*/
12+
@Path("/endpoint")
13+
public class MyResource {
14+
15+
@GET()
16+
@Path("/{id1}/{id2}")
17+
@Produces(MediaType.TEXT_PLAIN)
18+
public String get(@BeanParam MyPathParams pathParams, @BeanParam MyQueryParams queryParams) {
19+
return "/" + pathParams.getId1() + "/" + pathParams.getId2() + "?param1=" + queryParams.getParam1() + "&param2="
20+
+ queryParams.getParam2() + "&param3=" + queryParams.getParam3();
21+
}
22+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.javaee7.jaxrs.beanparam;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.net.MalformedURLException;
6+
import java.net.URI;
7+
import java.net.URL;
8+
9+
import javax.ws.rs.client.Client;
10+
import javax.ws.rs.client.ClientBuilder;
11+
import javax.ws.rs.client.WebTarget;
12+
13+
import org.jboss.arquillian.container.test.api.Deployment;
14+
import org.jboss.arquillian.junit.Arquillian;
15+
import org.jboss.arquillian.test.api.ArquillianResource;
16+
import org.jboss.shrinkwrap.api.ShrinkWrap;
17+
import org.jboss.shrinkwrap.api.spec.WebArchive;
18+
import org.junit.Before;
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
/**
23+
* @author Arun Gupta
24+
* @author Xavier Coulon
25+
*/
26+
@RunWith(Arquillian.class)
27+
public class MyResourceTest {
28+
29+
@Deployment(testable = false)
30+
public static WebArchive createDeployment() {
31+
return ShrinkWrap.create(WebArchive.class)
32+
.addClasses(MyApplication.class, MyResource.class, MyPathParams.class, MyQueryParams.class);
33+
}
34+
private static WebTarget target;
35+
36+
@ArquillianResource
37+
private URL base;
38+
39+
@Before
40+
public void setUpClass() throws MalformedURLException {
41+
Client client = ClientBuilder.newClient();
42+
target = client.target(URI.create(new URL(base, "webresources/endpoint").toExternalForm()));
43+
}
44+
45+
@Test
46+
public void testRequestWithAllParams() {
47+
WebTarget t = target.path("/123").path("/abc").queryParam("param1", "foo").queryParam("param2", "bar").queryParam("param3", "baz");
48+
String r = t.request(). get(String.class);
49+
assertEquals("/123/abc?param1=foo&param2=bar&param3=baz", r);
50+
}
51+
52+
}

jaxrs/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<module>async-client</module>
2020
<module>async-server</module>
2121
<module>beanvalidation</module>
22+
<module>beanparam</module>
2223
<module>client-negotiation</module>
2324
<module>dynamicfilter</module>
2425
<module>fileupload</module>

0 commit comments

Comments
 (0)