Skip to content

Commit 4ecf368

Browse files
committed
Added a very simple example for a JAX-RS GET request
1 parent c9027f8 commit 4ecf368

5 files changed

Lines changed: 135 additions & 1 deletion

File tree

jaxrs/pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<module>beanvalidation</module>
1818
<module>beanparam</module>
1919
<module>client-negotiation</module>
20+
<module>db-access</module>
2021
<module>dynamicfilter</module>
2122
<module>fileupload</module>
2223
<module>filter</module>
@@ -35,10 +36,11 @@
3536
<module>request-binding</module>
3637
<module>resource-validation</module>
3738
<module>server-negotiation</module>
39+
<module>simple-get</module>
3840
<module>singleton</module>
3941
<module>readerwriter-injection</module>
4042
<module>jaxrs-security-declarative</module>
41-
<module>db-access</module>
43+
4244
</modules>
4345

4446
<dependencies>

jaxrs/simple-get/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>org.javaee7</groupId>
6+
<artifactId>jaxrs</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>simple-get</artifactId>
11+
<packaging>war</packaging>
12+
13+
<name>Java EE 7 Sample: jaxrs - simple-get</name>
14+
15+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/** Copyright Payara Services Limited **/
2+
package org.javaee7.jaxrs.simple.get;
3+
4+
import javax.ws.rs.ApplicationPath;
5+
import javax.ws.rs.core.Application;
6+
7+
/**
8+
* This class activates JAX-RS and sets the base path to "/rest".
9+
*
10+
* @author Arjan Tijms
11+
*
12+
*/
13+
@ApplicationPath("/rest")
14+
public class JaxRsActivator extends Application {
15+
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/** Copyright Payara Services Limited **/
2+
3+
package org.javaee7.jaxrs.simple.get;
4+
5+
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
6+
7+
import javax.ws.rs.GET;
8+
import javax.ws.rs.Path;
9+
import javax.ws.rs.Produces;
10+
11+
/**
12+
* A very simple JAX-RS resource class that just returns the string "hi!"
13+
*
14+
* @author Arjan Tijms
15+
*
16+
*/
17+
@Path("/resource")
18+
@Produces(TEXT_PLAIN)
19+
public class Resource {
20+
21+
@GET
22+
@Path("hi")
23+
public String hi() {
24+
return "hi!";
25+
}
26+
27+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/** Copyright Payara Services Limited **/
2+
3+
package org.javaee7.jaxrs.simple.get;
4+
5+
import static javax.ws.rs.client.ClientBuilder.newClient;
6+
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
7+
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
8+
import static org.junit.Assert.assertTrue;
9+
10+
import java.io.IOException;
11+
import java.net.URI;
12+
import java.net.URL;
13+
14+
import org.javaee7.jaxrs.simple.get.JaxRsActivator;
15+
import org.javaee7.jaxrs.simple.get.Resource;
16+
import org.jboss.arquillian.container.test.api.Deployment;
17+
import org.jboss.arquillian.container.test.api.RunAsClient;
18+
import org.jboss.arquillian.junit.Arquillian;
19+
import org.jboss.arquillian.test.api.ArquillianResource;
20+
import org.jboss.shrinkwrap.api.spec.WebArchive;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
24+
/**
25+
* This sample tests one of the simplest possible JAX-RS resources; one that only
26+
* has a single method responding to a GET request and returning a (small) string.
27+
*
28+
* @author Arjan Tijms
29+
*/
30+
@RunWith(Arquillian.class)
31+
public class JAXRSSimpleGetTest {
32+
33+
@ArquillianResource
34+
private URL base;
35+
36+
@Deployment(testable = false)
37+
public static WebArchive createDeployment() {
38+
WebArchive archive =
39+
create(WebArchive.class)
40+
.addClasses(
41+
JaxRsActivator.class,
42+
Resource.class
43+
);
44+
45+
System.out.println("************************************************************");
46+
System.out.println(archive.toString(true));
47+
System.out.println("************************************************************");
48+
49+
return archive;
50+
}
51+
52+
@Test
53+
@RunAsClient
54+
public void testGet() throws IOException {
55+
56+
String response =
57+
newClient()
58+
.target(
59+
URI.create(new URL(base, "rest/resource/hi").toExternalForm()))
60+
.request(TEXT_PLAIN)
61+
.get(String.class);
62+
63+
System.out.println("-------------------------------------------------------------------------");
64+
System.out.println("Response: \n\n" + response);
65+
System.out.println("-------------------------------------------------------------------------");
66+
67+
assertTrue(
68+
response.contains("hi")
69+
);
70+
}
71+
72+
73+
74+
}

0 commit comments

Comments
 (0)