blob: 2b37e11ca94adc6ec81373bcc3e051ebaecba240 [file] [log] [blame]
<!--
/***************************************************************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
***************************************************************************************************************************/
-->
Resource Classes
<p>
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.
</p>
<p class='bpcode w800'>
<ja>@Rest</ja>(
path=<js>"/*"</js>,
title=<js>"My Microservice"</js>,
description=<js>"Top-level resources page"</js>,
htmldoc=<ja>@HtmlDoc</ja>(
navlinks={
<js>"options: servlet:/?method=OPTIONS"</js>
}
),
children={
HelloWorldResource.<jk>class</jk>,
ConfigResource.<jk>class</jk>,
LogsResource.<jk>class</jk>
}
)
<jk>public class</jk> RootResources <jk>extends</jk> BasicRestServletJenaGroup {
<jc>// No code! </jc>
}
</p>
<p>
When deployed, it looks like this in a browser:
</p>
<p class='bpcode w800'>
http://localhost:10000
</p>
<img class='bordered w800' src='doc-files/juneau-microservice-jetty.Running.1.png'>
<ul class='spaced-list'>
<li>
The </l>title</l> and <l>description</l> annotations define the titles on the page.
<br>These can be globalized using <l>$L{...}</l> variables, or by defining specially-named properties in the
properties file for the resource.
<li>
In this case, the <l>path</l> annotation defines the context root of your application since it was
not specified in the manifest or config file.
<br>Therefore, this resource is mapped to <l>http://localhost:10000</l>.
<li>
The <l>children</l> annotation make up the list of child resources.
<br>These child resources can be anything that extends from <l>Servlet</l>, although usually
they will be subclasses of {@link oajr.BasicRestServlet} or other resource groups.
</ul>
<p>
If you click the <l>helloWorld</l> link in your application, you'll get a simple hello world message:
</p>
<p class='bpcode w800'>
http://localhost:10000/helloWorld
</p>
<img class='bordered w800' src='doc-files/juneau-microservice-jetty.ResourceClasses.1.png'>
<p>
...which is generated by this class...
</p>
<p class='bpcode w800'>
<ja>@Rest</ja>(
path=<js>"/helloWorld"</js>,
title=<js>"Hello World example"</js>,
description=<js>"Simplest possible REST resource"</js>
)
<jk>public class</jk> HelloWorldResource <jk>extends</jk> BasicRestServlet {
<ja>@RestMethod</ja>(name=<jsf>GET</jsf>, path=<js>"/*"</js>)
<jk>public</jk> String sayHello() {
<jk>return</jk> <js>"Hello world!"</js>;
}
}
</p>
<p>
The most-common case for deploying the top-level resource is to use the {@link oaj.microservice.jetty.JettyMicroserviceBuilder#servlet(Class)} method:
</p>
<p class='bpcode w800'>
<jk>public class</jk> App {
<jk>public static void</jk> main(String[] args) {
JettyMicroservice
.<jsm>create</jsm>()
.args(args)
.servlet(RootResources.<jk>class</jk>) <jc>// Our root resource.</jc>
.build()
.start()
;
}
}
</p>
<p>
However, there are multiple ways of deploying top-level resources:
</p>
<ul class='spaced-list'>
<li>
{@link oaj.microservice.jetty.JettyMicroserviceBuilder#servlet(Class)} - Using the builder. Several methods provided.
<li>
{@link oaj.microservice.jetty.JettyMicroservice#addServlet(Servlet,String)} - After the Jetty container has been started.
<li>
As a configuration variable <js>"Jetty/servlets"</js>.
<p class='bcode w800'>
<cc>#=======================================================================================================================
# Jetty settings
#=======================================================================================================================</cc>
<cs>[Jetty]</cs>
<cc># Subclasses of RestServlet</cc>
<ck>servlets</ck> = <cv>org.apache.juneau.examples.rest.RootResources</cv>
</p>
<li>
As a configuration variable <js>"Jetty/servletMap"</js>.
<p class='bcode w800'>
<cc>#=======================================================================================================================
# Jetty settings
#=======================================================================================================================</cc>
<cs>[Jetty]</cs>
<cc># Any servlets and their path specs</cc>
<ck>servletMap</ck> =
<cv>{
'/*': 'org.apache.juneau.examples.rest.RootResources'
}</cv>
</cv>
<li>
Directly in the <c>jetty.xml</c> file.
<p class='bcode w800'>
<xt>&lt;Configure</xt> <xa>id</xa>=<xs>"ExampleServer"</xs> <xa>class</xa>=<xs>"org.eclipse.jetty.server.Server"</xs><xt>&gt;</xt>
...
<xt>&lt;New</xt> <xa>id</xa>=<xs>"context"</xs> <xa>class</xa>=<xs>"org.eclipse.jetty.servlet.ServletContextHandler"</xs><xt>&gt;</xt>
<xt>&lt;Set</xt> <xa>name</xa>=<xs>"contextPath"</xs><xt>&gt;/&lt;/Set&gt;</xt>
<xt>&lt;Call</xt> <xa>name</xa>=<xs>"addServlet"</xs><xt>&gt;</xt>
<xt>&lt;Arg&gt;</xt>org.apache.juneau.rest.test.Root<xt>&lt;/Arg&gt;</xt>
<xt>&lt;Arg&gt;</xt>/*<xt>&lt;/Arg&gt;</xt>
<xt>&lt;/Call&gt;</xt>
<xt>&lt;Set</xt> <xa>name</xa>=<xs>"sessionHandler"</xs><xt>&gt;</xt>
<xt>&lt;New</xt> <xa>class</xa>=<xs>"org.eclipse.jetty.server.session.SessionHandler"</xs> <xt>/&gt;</xt>
<xt>&lt;/Set&gt;</xt>
<xt>&lt;/New&gt;</xt>
...
</p>
</ul>