From 750aee9f1cdfbcbe08e3e52bb03b81e844291149 Mon Sep 17 00:00:00 2001 From: fabiovandewaeter <vandewaeter.fabio@gmail.com> Date: Sun, 9 Mar 2025 15:41:04 +0100 Subject: [PATCH] =?UTF-8?q?d=C3=A9but?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jersey-service/.gitignore | 1 + jersey-service/pom.xml | 82 +++++++++++++++++++ .../src/main/java/fil/sr2/flopbox/Main.java | 44 ++++++++++ .../main/java/fil/sr2/flopbox/MyResource.java | 25 ++++++ .../java/fil/sr2/flopbox/MyResourceTest.java | 48 +++++++++++ 5 files changed, 200 insertions(+) create mode 100644 jersey-service/.gitignore create mode 100644 jersey-service/pom.xml create mode 100644 jersey-service/src/main/java/fil/sr2/flopbox/Main.java create mode 100644 jersey-service/src/main/java/fil/sr2/flopbox/MyResource.java create mode 100644 jersey-service/src/test/java/fil/sr2/flopbox/MyResourceTest.java diff --git a/jersey-service/.gitignore b/jersey-service/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/jersey-service/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/jersey-service/pom.xml b/jersey-service/pom.xml new file mode 100644 index 0000000..fddd694 --- /dev/null +++ b/jersey-service/pom.xml @@ -0,0 +1,82 @@ +<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/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <groupId>fil.sr2.flopbox</groupId> + <artifactId>jersey-service</artifactId> + <packaging>jar</packaging> + <version>1.0-SNAPSHOT</version> + <name>jersey-service</name> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.glassfish.jersey</groupId> + <artifactId>jersey-bom</artifactId> + <version>${jersey.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + <dependency> + <groupId>org.glassfish.jersey.containers</groupId> + <artifactId>jersey-container-grizzly2-http</artifactId> + </dependency> + <dependency> + <groupId>org.glassfish.jersey.inject</groupId> + <artifactId>jersey-hk2</artifactId> + </dependency> + + <!-- uncomment this to get JSON support: + <dependency> + <groupId>org.glassfish.jersey.media</groupId> + <artifactId>jersey-media-json-binding</artifactId> + </dependency> + --> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.12</version> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.8.1</version> + <inherited>true</inherited> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>exec-maven-plugin</artifactId> + <version>1.2.1</version> + <executions> + <execution> + <goals> + <goal>java</goal> + </goals> + </execution> + </executions> + <configuration> + <mainClass>fil.sr2.flopbox.Main</mainClass> + </configuration> + </plugin> + </plugins> + </build> + + <properties> + <jersey.version>3.0.2</jersey.version> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> +</project> diff --git a/jersey-service/src/main/java/fil/sr2/flopbox/Main.java b/jersey-service/src/main/java/fil/sr2/flopbox/Main.java new file mode 100644 index 0000000..ecede9f --- /dev/null +++ b/jersey-service/src/main/java/fil/sr2/flopbox/Main.java @@ -0,0 +1,44 @@ +package fil.sr2.flopbox; + +import org.glassfish.grizzly.http.server.HttpServer; +import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; +import org.glassfish.jersey.server.ResourceConfig; + +import java.io.IOException; +import java.net.URI; + +/** + * Main class. + * + */ +public class Main { + // Base URI the Grizzly HTTP server will listen on + public static final String BASE_URI = "http://localhost:8080/"; + + /** + * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application. + * @return Grizzly HTTP server. + */ + public static HttpServer startServer() { + // create a resource config that scans for JAX-RS resources and providers + // in fil.sr2.flopbox package + final ResourceConfig rc = new ResourceConfig().packages("fil.sr2.flopbox"); + + // create and start a new instance of grizzly http server + // exposing the Jersey application at BASE_URI + return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc); + } + + /** + * Main method. + * @param args + * @throws IOException + */ + public static void main(String[] args) throws IOException { + final HttpServer server = startServer(); + System.out.println(String.format("Jersey app started with endpoints available at " + + "%s%nHit Ctrl-C to stop it...", BASE_URI)); + System.in.read(); + server.stop(); + } +} diff --git a/jersey-service/src/main/java/fil/sr2/flopbox/MyResource.java b/jersey-service/src/main/java/fil/sr2/flopbox/MyResource.java new file mode 100644 index 0000000..994c319 --- /dev/null +++ b/jersey-service/src/main/java/fil/sr2/flopbox/MyResource.java @@ -0,0 +1,25 @@ +package fil.sr2.flopbox; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; + +/** + * Root resource (exposed at "myresource" path) + */ +@Path("myresource") +public class MyResource { + + /** + * Method handling HTTP GET requests. The returned object will be sent + * to the client as "text/plain" media type. + * + * @return String that will be returned as a text/plain response. + */ + @GET + @Produces(MediaType.TEXT_PLAIN) + public String getIt() { + return "Got it!"; + } +} diff --git a/jersey-service/src/test/java/fil/sr2/flopbox/MyResourceTest.java b/jersey-service/src/test/java/fil/sr2/flopbox/MyResourceTest.java new file mode 100644 index 0000000..79e09ec --- /dev/null +++ b/jersey-service/src/test/java/fil/sr2/flopbox/MyResourceTest.java @@ -0,0 +1,48 @@ +package fil.sr2.flopbox; + +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.ClientBuilder; +import jakarta.ws.rs.client.WebTarget; + +import org.glassfish.grizzly.http.server.HttpServer; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class MyResourceTest { + + private HttpServer server; + private WebTarget target; + + @Before + public void setUp() throws Exception { + // start the server + server = Main.startServer(); + // create the client + Client c = ClientBuilder.newClient(); + + // uncomment the following line if you want to enable + // support for JSON in the client (you also have to uncomment + // dependency on jersey-media-json module in pom.xml and Main.startServer()) + // -- + // c.configuration().enable(new org.glassfish.jersey.media.json.JsonJaxbFeature()); + + target = c.target(Main.BASE_URI); + } + + @After + public void tearDown() throws Exception { + server.stop(); + } + + /** + * Test to see that the message "Got it!" is sent in the response. + */ + @Test + public void testGetIt() { + String responseMsg = target.path("myresource").request().get(String.class); + assertEquals("Got it!", responseMsg); + } +} -- GitLab