Follow these instructions to import the REST examples project using Jetty into Eclipse.
-
App.java - The entry point.
This class creates and starts our microservice:
public class App {
public static void main(String[] args) throws Exception {
JettyMicroservice
.create()
.args(args)
.servlet(RootResources.class)
.build()
.start()
.startConsole()
.join();
}
}
-
RootResources.java - The top-level REST resource.
This class routes HTTP requests to child resources:
@Rest(
path="/*",
title="Root resources",
description="Example of a router resource page.",
htmldoc=@HtmlDoc(
widgets={
ContentTypeMenuItem.class,
ThemeMenuItem.class
},
navlinks={
"options: ?method=OPTIONS",
"$W{ContentTypeMenuItem}",
"$W{ThemeMenuItem}",
"source: $C{Source/gitHub}/org/apache/juneau/examples/rest/$R{servletClassSimple}.java"
},
aside={
"<div style='max-width:400px' class='text'>",
" <p>This is an example of a 'router' page that serves as a jumping-off point to child resources.</p>",
" <p>Resources can be nested arbitrarily deep through router pages.</p>",
" <p>Note the <span class='link'>options</span> link provided that lets you see the generated swagger doc for this page.</p>",
" <p>Also note the <span class='link'>sources</span> link on these pages to view the source code for the page.</p>",
" <p>All content on pages in the UI are serialized POJOs. In this case, it's a serialized array of beans with 2 properties, 'name' and 'description'.</p>",
" <p>Other features (such as this aside) are added through annotations.</p>",
"</div>"
}
),
children={
HelloWorldResource.class,
PetStoreResource.class,
DtoExamples.class,
ConfigResource.class,
LogsResource.class,
ShutdownResource.class
}
)
@SerializerConfig(
// For testing purposes, we want to use single quotes in all the serializers so it's easier to do simple
// String comparisons.
quoteChar="'"
)
public class RootResources extends BasicRestServletJenaGroup {
// No code
}
-
juneau-examples-rest-jetty.cfg - The external configuration file.
Contains various useful settings.
Can be used for your own resource configurations.
#=======================================================================================================================
# Basic configuration file for REST microservices
# Subprojects can use this as a starting point.
#=======================================================================================================================
#=======================================================================================================================
# Jetty settings
#=======================================================================================================================
[Jetty]
# Path of the jetty.xml file used to configure the Jetty server.
config = jetty.xml
# Resolve Juneau variables in the jetty.xml file.
resolveVars = true
# Port to use for the jetty server.
# You can specify multiple ports. The first available will be used. '0' indicates to try a random port.
# The resulting available port gets set as the system property "availablePort" which can be referenced in the
# jetty.xml file as "$S{availablePort}" (assuming resolveVars is enabled).
port = 10000,0,0,0
# Optionally specify your servlets here:
#servlets = org.apache.juneau.microservice.sample.RootResources
#=======================================================================================================================
# REST settings
#=======================================================================================================================
[REST]
# Comma-delimited list of key-value pairs that represent locations of static files that can be served up by your @Rest-annotated
# classes. These are static files that are served up by the servlet under the specified sub-paths.
# For example, given the following setting...
# staticFiles = htdocs:my-docs,styles/my-styles
# ...the URI "/servletPath/htdocs/javadoc.css" resolves to the path "/my-docs/javadoc.css".
# This path can be relative to the working directory, classpath root, or package of your resource class.
# Used by the BasicRestConfig interface that defines the following value:
# staticFiles="$C{REST/staticFiles}"
staticFiles = htdocs:htdocs
# Stylesheet to use for HTML views.
# Used by the BasicRestConfig interface that defines the following value:
# stylesheet="$C{REST/theme,servlet:/htdocs/themes/devops.css}"
theme = servlet:/htdocs/themes/devops.css
# Various look-and-feel settings used in the BasicRestConfig interface.
headerIcon = servlet:/htdocs/images/juneau.png
headerLink = http://juneau.apache.org
footerIcon = servlet:/htdocs/images/asf.png
footerLink = http://www.apache.org
favicon = $C{REST/headerIcon}
header =
<a href='$U{$C{REST/headerLink}}'>
<img src='$U{$C{REST/headerIcon}}' style='position:absolute;top:5;right:5;background-color:transparent;height:30px'/>
</a>
footer =
<a href='$U{$C{REST/footerLink}}'>
<img src='$U{$C{REST/footerIcon}}' style='float:right;padding-right:20px;height:32px'/>
</a>
#=======================================================================================================================
# Console settings
#=======================================================================================================================
[Console]
enabled = true
# List of available console commands.
# These are classes that implements ConsoleCommand that allow you to submit commands to the microservice via
# the console.
# When listed here, the implementations must provide a no-arg constructor.
# They can also be provided dynamically by overriding the Microservice.createConsoleCommands() method.
commands =
org.apache.juneau.microservice.console.ExitCommand,
org.apache.juneau.microservice.console.RestartCommand,
org.apache.juneau.microservice.console.HelpCommand,
org.apache.juneau.microservice.console.ConfigCommand
#=======================================================================================================================
# Logger settings
#-----------------------------------------------------------------------------------------------------------------------
# See FileHandler Java class for details.
#=======================================================================================================================
[Logging]
...
#=======================================================================================================================
# System properties
#-----------------------------------------------------------------------------------------------------------------------
# These are arbitrary system properties that are set during startup.
#=======================================================================================================================
[SystemProperties]
# Configure Jetty for StdErrLog Logging
# org.eclipse.jetty.util.log.class = org.eclipse.jetty.util.log.StrErrLog
# Configure Jetty to log using java-util logging
org.eclipse.jetty.util.log.class = org.apache.juneau.microservice.jetty.JettyLogger
# Jetty logging level
# Possible values: ALL, DEBUG, INFO, WARN, OFF
org.eclipse.jetty.LEVEL = WARN
derby.stream.error.file = $C{Logging/logDir}/derby-errors.log
-
jetty.xml - The Jetty configuration file.
A bare-bones config file that can be extended to use any Jetty features.
<Configure id="ExampleServer" class="org.eclipse.jetty.server.Server">
<Set name="connectors">
<Array type="org.eclipse.jetty.server.Connector">
<Item>
<New class="org.eclipse.jetty.server.ServerConnector">
<Arg>
<Ref refid="ExampleServer"/>
</Arg>
<Set name="port">$S{availablePort,8080}</Set>
</New>
</Item>
</Array>
</Set>
<New id="context" class="org.eclipse.jetty.servlet.ServletContextHandler">
<Set name="contextPath">/</Set>
<!-- Optionally specify your servlets here -->
<!--Call name="addServlet">
<Arg>org.apache.juneau.microservice.sample.RootResources</Arg>
<Arg>/*</Arg>
</Call-->
<Set name="sessionHandler">
<New class="org.eclipse.jetty.server.session.SessionHandler"/>
</Set>
</New>
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.HandlerCollection">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<Ref refid="context"/>
</Item>
<Item>
<New class="org.eclipse.jetty.server.handler.DefaultHandler"/>
</Item>
</Array>
</Set>
</New>
</Set>
<Set name="requestLog">
<New id="RequestLogImpl" class="org.eclipse.jetty.server.NCSARequestLog">
<Set name="filename"><Property name="jetty.logs" default="$C{Logging/logDir,logs}"/>/jetty-requests.log</Set>
<Set name="filenameDateFormat">yyyy_MM_dd</Set>
<Set name="LogTimeZone">GMT</Set>
<Set name="retainDays">90</Set>
<Set name="append">false</Set>
<Set name="LogLatency">true</Set>
</New>
</Set>
<Get name="ThreadPool">
<Set name="minThreads" type="int">10</Set>
<Set name="maxThreads" type="int">100</Set>
<Set name="idleTimeout" type="int">60000</Set>
<Set name="detailedDump">true</Set>
</Get>
</Configure>
At this point, you're ready to start the microservice from your workspace.