Resource Classes
This section describes how to define a top-level REST resource page and deploy it in our microservice. The example is a router page that serves as a jumping off page to child resources.
@Rest(
path="/*",
title="My Microservice",
description="Top-level resources page",
htmldoc=@HtmlDoc(
navlinks={
"options: servlet:/?method=OPTIONS"
}
),
children={
HelloWorldResource.class,
ConfigResource.class,
LogsResource.class
}
)
public class RootResources extends BasicRestServletGroup {
// No code!
}
When deployed, it looks like this in a browser:
http://localhost:10000
-
The title and description annotations define the titles on the page. These can be globalized using
$L{...}
variables, or by defining specially-named properties in the properties file for the resource. -
In this case, the
path
annotation defines the context root of your application since it was not specified in the manifest or config file. Therefore, this resource is mapped tohttp://localhost:10000
. -
The
children
annotation make up the list of child resources. These child resources can be anything that extends fromServlet
, although usually they will be subclasses of BasicRestServlet or other resource groups.
If you click the helloWorld
link in your application, you'll get a simple hello world message:
http://localhost:10000/helloWorld
...which is generated by this class...
@Rest(
path="/helloWorld",
title="Hello World example",
description="Simplest possible REST resource"
)
public class HelloWorldResource extends BasicRestServlet {
@RestGet("/*")
public String sayHello() {
return "Hello world!";
}
}
The most-common case for deploying the top-level resource is to use the JettyMicroservice.Builder.servlet(Class) method:
public class App {
public static void main(String[] args) {
JettyMicroservice
.create()
.args(args)
.servlet(RootResources.class) // Our root resource.
.build()
.start()
;
}
}
However, there are multiple ways of deploying top-level resources:
- JettyMicroservice.Builder.servlet(Class) - Using the builder. Several methods provided.
- JettyMicroservice.addServlet(Servlet,String) - After the Jetty container has been started.
- As a configuration variable
Jetty/servlets
.
#=======================================================================================================================
# Jetty settings
#=======================================================================================================================
[Jetty]
# Subclasses of RestServlet
servlets = org.apache.juneau.examples.rest.RootResources
- As a configuration variable
Jetty/servletMap
.
#=======================================================================================================================
# Jetty settings
#=======================================================================================================================
[Jetty]
# Any servlets and their path specs
servletMap =
{
'/*': 'org.apache.juneau.examples.rest.RootResources'
}
- Directly in the
jetty.xml
file.
<Configure id="ExampleServer" class="org.eclipse.jetty.server.Server">
...
<New id="context" class="org.eclipse.jetty.ee9.servlet.ServletContextHandler">
<Set name="contextPath">/</Set>
<Call name="addServlet">
<Arg>org.apache.juneau.rest.test.Root</Arg>
<Arg>/*</Arg>
</Call>
<Set name="sessionHandler">
<New class="org.eclipse.jetty.ee9.nested.SessionHandler" />
</Set>
</New>
...