| <!-- |
| /*************************************************************************************************************************** |
| * 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. |
| ***************************************************************************************************************************/ |
| --> |
| |
| Installing in Eclipse |
| |
| <p> |
| Follow these instructions to import the REST examples project using Jetty into Eclipse. |
| </p> |
| <ol class='spaced-list'> |
| <li> |
| Download the <c>juneau-examples-rest-jetty-{@property juneauVersion}.zip</c> file from the downloads page |
| (located in the binaries) and import it into your workspace as an existing project: |
| <br><br> |
| <img class='bordered' src='doc-files/juneau-examples-rest-jetty.Installing.1.png' style='width:524px'> |
| <li> |
| Select the archive file and import the project: |
| <br><br> |
| <img class='bordered' src='doc-files/juneau-examples-rest-jetty.Installing.2.png' style='width:549px'> |
| <li> |
| In your workspace, you should now see the following project: |
| <br><br> |
| <img class='bordered' src='doc-files/juneau-examples-rest-jetty.Installing.3.png' style='width:400px'> |
| </ol> |
| <p> |
| The important elements in this project are: |
| </p> |
| <ul class='spaced-list'> |
| <li> |
| <l>App.java</l> - The entry point. |
| <br>This class creates and starts our microservice: |
| <br><br> |
| <p class='bcode w800'> |
| <jk>public class</jk> App { |
| |
| <jk>public static void</jk> main(String[] args) <jk>throws</jk> Exception { |
| JettyMicroservice |
| .<jsm>create</jsm>() |
| .args(args) |
| .servlet(RootResources.<jk>class</jk>) |
| .build() |
| .start() |
| .startConsole() |
| .join(); |
| } |
| } |
| </p> |
| <li> |
| <l>RootResources.java</l> - The top-level REST resource. |
| <br>This class routes HTTP requests to child resources: |
| <br><br> |
| <p class='bcode w800'> |
| <ja>@Rest</ja>( |
| path=<js>"/*"</js>, |
| title=<js>"Root resources"</js>, |
| description=<js>"Example of a router resource page."</js>, |
| htmldoc=<ja>@HtmlDoc</ja>( |
| widgets={ |
| ContentTypeMenuItem.<jk>class</jk>, |
| ThemeMenuItem.<jk>class</jk> |
| }, |
| navlinks={ |
| <js>"options: ?method=OPTIONS"</js>, |
| <js>"$W{ContentTypeMenuItem}"</js>, |
| <js>"$W{ThemeMenuItem}"</js>, |
| <js>"source: $C{Source/gitHub}/org/apache/juneau/examples/rest/$R{servletClassSimple}.java"</js> |
| }, |
| aside={ |
| <js>"<div style='max-width:400px' class='text'>"</js>, |
| <js>" <p>This is an example of a 'router' page that serves as a jumping-off point to child resources.</p>"</js>, |
| <js>" <p>Resources can be nested arbitrarily deep through router pages.</p>"</js>, |
| <js>" <p>Note the <span class='link'>options</span> link provided that lets you see the generated swagger doc for this page.</p>"</js>, |
| <js>" <p>Also note the <span class='link'>sources</span> link on these pages to view the source code for the page.</p>"</js>, |
| <js>" <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>"</js>, |
| <js>" <p>Other features (such as this aside) are added through annotations.</p>"</js>, |
| <js>"</div>"</js> |
| } |
| ), |
| children={ |
| HelloWorldResource.<jk>class</jk>, |
| PetStoreResource.<jk>class</jk>, |
| DtoExamples.<jk>class</jk>, |
| ConfigResource.<jk>class</jk>, |
| LogsResource.<jk>class</jk>, |
| ShutdownResource.<jk>class</jk> |
| } |
| ) |
| <ja>@SerializerConfig</ja>( |
| <jc>// For testing purposes, we want to use single quotes in all the serializers so it's easier to do simple |
| // String comparisons.</jc> |
| quoteChar=<js>"'"</js> |
| ) |
| <jk>public class</jk> RootResources <jk>extends</jk> BasicRestServletJenaGroup { |
| <jc>// No code</jc> |
| } |
| </p> |
| <li> |
| <l>juneau-examples-rest-jetty.cfg</l> - The external configuration file. |
| <br>Contains various useful settings. |
| <br>Can be used for your own resource configurations. |
| <br><br> |
| <p class='bcode w800'> |
| <cc>#======================================================================================================================= |
| # Basic configuration file for REST microservices |
| # Subprojects can use this as a starting point. |
| #=======================================================================================================================</cc> |
| |
| <cc>#======================================================================================================================= |
| # Jetty settings |
| #=======================================================================================================================</cc> |
| <cs>[Jetty]</cs> |
| |
| <cc># Path of the jetty.xml file used to configure the Jetty server.</cc> |
| <ck>config</ck> = <cv>jetty.xml</cv> |
| |
| <cc># Resolve Juneau variables in the jetty.xml file.</cc> |
| <ck>resolveVars</ck> = <cv>true</cv> |
| |
| <cc># 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).</cc> |
| <ck>port</ck> = <cv>10000,0,0,0</cv> |
| |
| <cc># Optionally specify your servlets here: |
| #servlets = org.apache.juneau.microservice.sample.RootResources</cc> |
| |
| <cc>#======================================================================================================================= |
| # REST settings |
| #=======================================================================================================================</cc> |
| <cs>[REST]</cs> |
| |
| <cc># 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}"</cc> |
| <ck>staticFiles</ck> = htdocs:htdocs |
| |
| <cc># 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}"</cc> |
| <ck>theme</ck> = <cv>servlet:/htdocs/themes/devops.css</cv> |
| |
| <cc># Various look-and-feel settings used in the BasicRestConfig interface.</cc> |
| <ck>headerIcon</ck> = <cv>servlet:/htdocs/images/juneau.png</cv> |
| <ck>headerLink</ck> = <cv>http://juneau.apache.org</cv> |
| <ck>footerIcon</ck> = <cv>servlet:/htdocs/images/asf.png</cv> |
| <ck>footerLink</ck> = <cv>http://www.apache.org</cv> |
| <ck>favicon</ck> = <cv>$C{REST/headerIcon}</cv> |
| <ck>header</ck> = |
| <cv><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></cv> |
| <ck>footer</ck> = |
| <cv><a href='$U{$C{REST/footerLink}}'> |
| <img src='$U{$C{REST/footerIcon}}' style='float:right;padding-right:20px;height:32px'/> |
| </a></cv> |
| |
| <cc>#======================================================================================================================= |
| # Console settings |
| #=======================================================================================================================</cc> |
| <cs>[Console]</cs> |
| |
| <ck>enabled</ck> = <cv>true</cv> |
| |
| <cc># 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.</cc> |
| <ck>commands</ck> = |
| <cv>org.apache.juneau.microservice.console.ExitCommand, |
| org.apache.juneau.microservice.console.RestartCommand, |
| org.apache.juneau.microservice.console.HelpCommand, |
| org.apache.juneau.microservice.console.ConfigCommand</cv> |
| |
| <cc>#======================================================================================================================= |
| # Logger settings |
| #----------------------------------------------------------------------------------------------------------------------- |
| # See FileHandler Java class for details. |
| #=======================================================================================================================</cc> |
| <cs>[Logging]</cs> |
| |
| ... |
| |
| <cc>#======================================================================================================================= |
| # System properties |
| #----------------------------------------------------------------------------------------------------------------------- |
| # These are arbitrary system properties that are set during startup. |
| #=======================================================================================================================</cc> |
| <cs>[SystemProperties]</cs> |
| |
| <cc># Configure Jetty for StdErrLog Logging |
| # org.eclipse.jetty.util.log.class = org.eclipse.jetty.util.log.StrErrLog</cc> |
| |
| <cc># Configure Jetty to log using java-util logging</cc> |
| <ck>org.eclipse.jetty.util.log.class</ck> = <cv>org.apache.juneau.microservice.jetty.JettyLogger</cv> |
| |
| <cc># Jetty logging level |
| # Possible values: ALL, DEBUG, INFO, WARN, OFF</cc> |
| <ck>org.eclipse.jetty.LEVEL</ck> = <cv>WARN |
| |
| <ck>derby.stream.error.file</ck> = <cv>$C{Logging/logDir}/derby-errors.log</cv> |
| </p> |
| <li> |
| <l>jetty.xml</l> - The Jetty configuration file. |
| <br>A bare-bones config file that can be extended to use any Jetty features. |
| <br><br> |
| <p class='bcode w800'> |
| <xt><Configure</xt> <xa>id</xa>=<xs>"ExampleServer"</xs> <xa>class</xa>=<xs>"org.eclipse.jetty.server.Server"</xs>></xt> |
| |
| <xt><Set</xt> <xa>name</xa>=<xs>"connectors"</xs><xt>></xt> |
| <xt><Array</xt> <xa>type</xa>=<xs>"org.eclipse.jetty.server.Connector"</xs><xt>></xt> |
| <xt><Item></xt> |
| <xt><New</xt> <xa>class</xa>=<xs>"org.eclipse.jetty.server.ServerConnector"</xs><xt>></xt> |
| <xt><Arg></xt> |
| <xt><Ref</xt> <xa>refid</xa>=<xs>"ExampleServer"</xs><xt>/></xt> |
| <xt></Arg></xt> |
| <xt><Set</xt> <xa>name</xa>=<xs>"port"</xs><xt>></xt>$S{availablePort,8080}<xt></Set></xt> |
| <xt></New></xt> |
| <xt></Item></xt> |
| <xt></Array></xt> |
| <xt></Set></xt> |
| |
| <xt><New</xt> <xa>id</xa>=<xs>"context"</xs> <xa>class</xa>=<xs>"org.eclipse.jetty.servlet.ServletContextHandler"</xs><xt>></xt> |
| <xt><Set</xt> <xa>name</xa>=<xs>"contextPath"</xs><xt>>/</Set></xt> |
| <xc><!-- Optionally specify your servlets here --> |
| <!--Call name="addServlet"> |
| <Arg>org.apache.juneau.microservice.sample.RootResources</Arg> |
| <Arg>/*</Arg> |
| </Call--></xc> |
| <xt><Set</xt> <xa>name</xa>=<xs>"sessionHandler"</xs><xt>></xt> |
| <xt><New</xt> <xa>class</xa>=<xs>"org.eclipse.jetty.server.session.SessionHandler"</xs><xt>/></xt> |
| <xt></Set></xt> |
| <xt></New></xt> |
| |
| <xt><Set</xt> <xa>name</xa>=<xs>"handler"</xs><xt>></xt> |
| <xt><New</xt> <xa>class</xa>=<xs>"org.eclipse.jetty.server.handler.HandlerCollection"</xs><xt>></xt> |
| <xt><Set</xt> <xa>name</xa>=<xs>"handlers"</xs><xt>></xt> |
| <xt><Array</xt> <xa>type</xa>=<xs>"org.eclipse.jetty.server.Handler"</xs><xt>></xt> |
| <xt><Item></xt> |
| <xt><Ref</xt> <xa>refid</xa>=<xs>"context"</xs><xt>/></xt> |
| <xt></Item></xt> |
| <xt><Item></xt> |
| <xt><New</xt> <xa>class</xa>=<xs>"org.eclipse.jetty.server.handler.DefaultHandler"</xs><xt>/></xt> |
| <xt></Item></xt> |
| <xt></Array></xt> |
| <xt></Set></xt> |
| <xt></New></xt> |
| <xt></Set></xt> |
| |
| <xt><Set</xt> <xa>name</xa>=<xs>"requestLog"</xs><xt>></xt> |
| <xt><New</xt> <xa>id</xa>=<xs>"RequestLogImpl"</xs> <xa>class</xa>=<xs>"org.eclipse.jetty.server.NCSARequestLog"</xs><xt>></xt> |
| <xt><Set</xt> <xa>name</xa>=<xs>"filename"</xs><xt>><Property</xt> <xa>name</xa>=<xs>"jetty.logs"</xs> <xa>default</xa>=<xs>"$C{Logging/logDir,logs}"</xs><xt>/></xt>/jetty-requests.log<xt></Set></xt> |
| <xt><Set</xt> <xa>name</xa>=<xs>"filenameDateFormat</xs><xt>"></xt>yyyy_MM_dd<xt></Set></xt> |
| <xt><Set</xt> <xa>name</xa>=<xs>"LogTimeZone"</xs><xt>></xt>GMT<xt></Set></xt> |
| <xt><Set</xt> <xa>name</xa>=<xs>"retainDays"</xs><xt>></xt>90<xt></Set></xt> |
| <xt><Set</xt> <xa>name</xa>=<xs>"append"</xs><xt>></xt>false<xt></Set></xt> |
| <xt><Set</xt> <xa>name</xa>=<xs>"LogLatency"</xs><xt>></xt>true<xt></Set></xt> |
| <xt></New></xt> |
| <xt></Set></xt> |
| |
| <xt><Get</xt> <xa>name</xa>=<xs>"ThreadPool"</xs><xt>></xt> |
| <xt><Set</xt> <xa>name</xa>=<xs>"minThreads"</xs> <xa>type</xa>=<xs>"int"</xs><xt>></xt>10<xt></Set></xt> |
| <xt><Set</xt> <xa>name</xa>=<xs>"maxThreads"</xs> <xa>type</xa>=<xs>"int"</xs><xt>></xt>100<xt></Set></xt> |
| <xt><Set</xt> <xa>name</xa>=<xs>"idleTimeout"</xs> <xa>type</xa>=<xs>"int"</xs><xt>></xt>60000<xt></Set></xt> |
| <xt><Set</xt> <xa>name</xa>=<xs>"detailedDump"</xs><xt>></xt>true<xt></Set></xt> |
| <xt></Get></xt> |
| <xt></Configure></xt> |
| </p> |
| </ul> |
| <p> |
| At this point, you're ready to start the microservice from your workspace. |
| </p> |