Skip to content

Commit

Permalink
Publishing documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Automated publish committed Sep 5, 2024
1 parent aecbbc1 commit 3387305
Show file tree
Hide file tree
Showing 25 changed files with 354 additions and 1,333 deletions.
352 changes: 352 additions & 0 deletions snapshot/coreprofile/create_a_json_rest_service.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,352 @@
<!DOCTYPE html>


<!--
| Generated by Apache Maven Doxia Site Renderer 2.0.0-M19 from src/site/markdown/coreprofile/create_a_json_rest_service.md at 2024-09-05
| Rendered using Apache Maven Fluido Skin 2.0.0-M10
-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="generator" content="Apache Maven Doxia Site Renderer 2.0.0-M19" />
<title>Create a JSON REST service – Piranha</title>
<link rel="stylesheet" href="../css/apache-maven-fluido-2.0.0-M10.min.css" />
<link rel="stylesheet" href="../css/site.css" />
<link rel="stylesheet" href="../css/print.css" media="print" />
<script src="../js/apache-maven-fluido-2.0.0-M10.min.js"></script>
<style>.github-fork-ribbon:before { background-color: black; }</style>
</head>
<body>
<a class="github-fork-ribbon right-bottom fixed" href="https://github.com/piranhacloud/piranha" data-ribbon="Fork me on GitHub">Fork me on GitHub</a>
<div class="container-fluid container-fluid-top">
<header>
<div id="banner">
<div class="pull-left"></div>
<div class="pull-right"></div>
<div class="clear"><hr/></div>
</div>

<div id="breadcrumbs">
<ul class="breadcrumb">
<li id="projectVersion">Version: 24.9.0-SNAPSHOT</li>
<li id="publishDate" class="pull-right">Last Published: 2024-09-05</li>
</ul>
</div>
</header>
<div class="row-fluid">
<header id="leftColumn" class="span2">
<nav class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">Overview</li>
<li><a href="../getting-started.html">Getting Started</a></li>
</ul>
</nav>
<div class="well sidebar-nav">
<div id="poweredBy">
<div class="clear"></div>
<div id="twitter" style="border:none; margin-top: 10px">
<a href="https://twitter.com/piranha_cloud" class="twitter-follow-button" data-show-count="false" data-align="left" data-size="medium" data-show-screen-name="false" data-lang="en" data-dnt="true" >Follow piranha_cloud</a>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</div>
<div class="clear"></div>
<a href="https://maven.apache.org/" class="builtBy" target="_blank"><img class="builtBy" alt="Built by Maven" src="../images/logos/maven-feather.png" /></a>
</div>
</div>
</header>
<main id="bodyColumn" class="span10">
<section><a id="Create_a_JSON_REST_service"></a>
<h1>Create a JSON REST service</h1>
<p>If you are looking to create a JSON based REST service with Piranha then
consider Piranha Core Profile. It features a runtime ideally suited for REST,
JSON and micro services.</p>
<p>In 7 steps you will learn how to create the JSON REST service. They are:</p>
<ol style="list-style-type: decimal;">

<li>Create the Maven POM file</li>
<li>Add the application class</li>
<li>Add the POJO class</li>
<li>Add the endpoint</li>
<li>Add an integration test</li>
<li>Test the application</li>
<li>Deploy the application</li>
</ol><section><a id="Create_the_Maven_POM_file"></a>
<h2>Create the Maven POM file</h2>
<p>Create an empty directory to store your Maven project. Inside of that directory create the <code>pom.xml</code> file with the content as below.</p>

<pre class="prettyprint"><code class="language-xml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;project
xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;cloud.piranha.guides.coreprofile&lt;/groupId&gt;
&lt;artifactId&gt;json&lt;/artifactId&gt;
&lt;version&gt;1-SNAPSHOT&lt;/version&gt;
&lt;packaging&gt;war&lt;/packaging&gt;
&lt;name&gt;Piranha Core Profile - Temperature JSON service&lt;/name&gt;
&lt;properties&gt;
&lt;jakartaee.version&gt;10.0.0&lt;/jakartaee.version&gt;
&lt;java.version&gt;17&lt;/java.version&gt;
&lt;junit.version&gt;5.10.0-M1&lt;/junit.version&gt;
&lt;maven-compiler-plugin.version&gt;3.11.0&lt;/maven-compiler-plugin.version&gt;
&lt;maven-failsafe-plugin.version&gt;3.0.0&lt;/maven-failsafe-plugin.version&gt;
&lt;maven-war-plugin.version&gt;3.3.2&lt;/maven-war-plugin.version&gt;
&lt;piranha.version&gt;23.6.0&lt;/piranha.version&gt;
&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;jakarta.platform&lt;/groupId&gt;
&lt;artifactId&gt;jakarta.jakartaee-core-api&lt;/artifactId&gt;
&lt;version&gt;${jakartaee.version}&lt;/version&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
&lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
&lt;version&gt;${junit.version}&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
&lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
&lt;version&gt;${junit.version}&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
&lt;artifactId&gt;junit-jupiter-params&lt;/artifactId&gt;
&lt;version&gt;${junit.version}&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;finalName&gt;temperature&lt;/finalName&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;cloud.piranha.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;piranha-maven-plugin&lt;/artifactId&gt;
&lt;version&gt;${piranha.version}&lt;/version&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;id&gt;pre-integration-test&lt;/id&gt;
&lt;phase&gt;pre-integration-test&lt;/phase&gt;
&lt;goals&gt;
&lt;goal&gt;start&lt;/goal&gt;
&lt;/goals&gt;
&lt;/execution&gt;
&lt;execution&gt;
&lt;id&gt;post-integration-test&lt;/id&gt;
&lt;phase&gt;post-integration-test&lt;/phase&gt;
&lt;goals&gt;
&lt;goal&gt;stop&lt;/goal&gt;
&lt;/goals&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;version&gt;${maven-compiler-plugin.version}&lt;/version&gt;
&lt;configuration&gt;
&lt;release&gt;${java.version}&lt;/release&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-failsafe-plugin&lt;/artifactId&gt;
&lt;version&gt;${maven-failsafe-plugin.version}&lt;/version&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;goals&gt;
&lt;goal&gt;integration-test&lt;/goal&gt;
&lt;goal&gt;verify&lt;/goal&gt;
&lt;/goals&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
&lt;version&gt;${maven-war-plugin.version}&lt;/version&gt;
&lt;configuration&gt;
&lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;
</code></pre></section><section><a id="Add_the_application_class"></a>
<h2>Add the application class</h2>
<p>Add the Application class in the <code>src/main/java</code> directory, which allows you to
set the application path using the @ApplicationPath annotation.</p>

<pre class="prettyprint"><code class="language-java">package temperature;

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;

@ApplicationPath(&quot;&quot;)
public class TemperatureApplication extends Application {
}
</code></pre></section><section><a id="Add_the_POJO"></a>
<h2>Add the POJO</h2>
<p>As we are going to be returning JSON in the endpoint we need a POJO to represent
the model. Add the POJO class in the <code>src/main/java</code> directory.</p>

<pre class="prettyprint"><code class="language-java">package temperature;

public class Temperature {

public enum TemperatureScale {
CELSIUS,
FAHRENHEIT
}

private TemperatureScale scale;

private double temperature;

public TemperatureScale getScale() {
return scale;
}

public double getTemperature() {
return temperature;
}

public void setScale(TemperatureScale scale) {
this.scale = scale;
}

public void setTemperature(double temperature) {
this.temperature = temperature;
}
}
</code></pre></section><section><a id="Add_the_endpoint"></a>
<h2>Add the endpoint</h2>
<p>And the next step is creating the class with the <code>celsius</code> and <code>fahrenheit</code>
endpoints.</p>

<pre class="prettyprint"><code class="language-java">package temperature;

import jakarta.enterprise.context.RequestScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
import static temperature.Temperature.TemperatureScale.CELSIUS;
import static temperature.Temperature.TemperatureScale.FAHRENHEIT;

@RequestScoped
@Path(&quot;&quot;)
public class TemperatureBean {

@GET
@Produces(APPLICATION_JSON)
@Path(&quot;/celsius/{celsius}&quot;)
public Temperature celsius(@PathParam(&quot;celsius&quot;) double celsius) {
Temperature temp = new Temperature();
temp.setScale(CELSIUS);
temp.setTemperature(celsius);
return temp;
}

@GET
@Produces(&quot;application/json&quot;)
@Path(&quot;/fahrenheit/{fahrenheit}&quot;)
public Temperature fahrenheit(@PathParam(&quot;fahrenheit&quot;) double fahrenheit) {
Temperature temp = new Temperature();
temp.setScale(FAHRENHEIT);
temp.setTemperature(fahrenheit);
return temp;
}
}
</code></pre></section><section><a id="Add_an_integration_test"></a>
<h2>Add an integration test</h2>
<p>As we want to make sure the application gets tested before we release 2
integration tests are added which will be executed as part of the build.</p>
<p>We'll add the 2 integration tests to the <code>src/test/java</code> directory.</p>

<pre class="prettyprint"><code class="language-java">package temperature;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

class TemperatureIT {

@Test
void testCelsius() throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest
.newBuilder(new URI(&quot;http://localhost:8080/temperature/celsius/18.5&quot;))
.build();
HttpResponse&lt;String&gt; response = client.send(request, BodyHandlers.ofString());
assertEquals(&quot;{\&quot;scale\&quot;:\&quot;CELSIUS\&quot;,\&quot;temperature\&quot;:18.5}&quot;, response.body());
}

@Test
void testFahrenheit() throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest
.newBuilder(new URI(&quot;http://localhost:8080/temperature/fahrenheit/68.0&quot;))
.build();
HttpResponse&lt;String&gt; response = client.send(request, BodyHandlers.ofString());
assertEquals(&quot;{\&quot;scale\&quot;:\&quot;FAHRENHEIT\&quot;,\&quot;temperature\&quot;:68.0}&quot;, response.body());
}
}
</code></pre></section><section><a id="Test_the_application"></a>
<h2>Test the application</h2>
<p>The application is setup to use JUnit to do integration testing using the
Piranha Maven plugin so when you are building the application it will also
execute an integration test validating the endpoint works.</p>
<p>To build and test the application execute the following command:</p>

<pre class="prettyprint"><code class="language-bash"> mvn install
</code></pre></section><section><a id="Deploy_the_application"></a>
<h2>Deploy the application</h2>
<p>To deploy your application you will need 2 pieces.</p>
<ol style="list-style-type: decimal;">

<li>The Piranha Core Profile runtime JAR.</li>
<li>The WAR file you just produced.</li>
</ol>
<p>For the WAR file see the <code>target</code> directory. For the Piranha Core Profile
distribution go to Maven Central. And then the following command line will
deploy your application:</p>

<pre class="prettyprint"><code class="language-bash"> java -jar piranha-dist-coreprofile.jar --war-file json.war
</code></pre></section><section><a id="Conclusion"></a>
<h2>Conclusion</h2>
<p>As illustrated, creating a JSON based REST service with Piranha Core Profile is
pretty straightforward because of the libaries available to you as part of the
Jakarta EE Core Profile.</p></section><section><a id="References"></a>
<h2>References</h2>
<ol style="list-style-type: decimal;">

<li><a href="index.html">Piranha Core Profile</a></li>
<li><a href="../maven-plugin/index.html">Piranha Maven plugin documentation</a></li>
</ol>
<p><a href="index.html">Up</a></p></section></section> </main>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row-fluid">
<p class="pull-right">© 2024
<a href="https://piranha.cloud">Piranha Cloud</a>
</p>
</div>
</div>
</footer>
</body>
</html>
4 changes: 2 additions & 2 deletions snapshot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


<!--
| Generated by Apache Maven Doxia Site Renderer 2.0.0-M19 from src/site/markdown/index.md at 2024-08-31
| Generated by Apache Maven Doxia Site Renderer 2.0.0-M19 from src/site/markdown/index.md at 2024-09-05
| Rendered using Apache Maven Fluido Skin 2.0.0-M10
-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
Expand Down Expand Up @@ -30,7 +30,7 @@
<div id="breadcrumbs">
<ul class="breadcrumb">
<li id="projectVersion">Version: 24.9.0-SNAPSHOT</li>
<li id="publishDate" class="pull-right">Last Published: 2024-08-31</li>
<li id="publishDate" class="pull-right">Last Published: 2024-09-05</li>
</ul>
</div>
</header>
Expand Down
Loading

0 comments on commit 3387305

Please sign in to comment.