The Juneau REST servlet APIs are designed to work seemlessly with the Spring Boot framework.
The only restriction is that your top-level REST resource must extend from one of the following classes:
- {@link oajr.springboot}
- {@link oajr.springboot.BasicSpringRestServlet} - Basic servlet
- {@link oajr.springboot.BasicSpringRestServletGroup} - Basic servlet group
These classes are the equivalent to the {@link oajr.servlet.BasicRestServlet} and {@link oajr.servlet.BasicRestServletGroup}
except they hook into the injection framework of Spring Boot to provide resolution of beans (e.g. child resources, various
configuration classes).
The org.apache.juneau.examples.rest.springboot package and org.apache.juneau.examples.rest.springboot.App
application are a basic Spring Boot application that shows off simple Juneau examples including injection beans.
| @SpringBootApplication
| @Controller
| public class App {
|
| //Entry point method.
| public static void main(String[] args) {
| new SpringApplicationBuilder(App.class).run(args);
| }
|
| // Our root REST bean.
| // Note that this must extend from {@link oajr.springboot.SpringRestServlet} to allow use of injection.
| // All REST objects are attached to this bean using the {@link oajr.annotation.Rest#children()} annotation.
| @Bean
| public RootResources getRootResources() {
| return new RootResources();
| }
|
| // Registers our REST bean at the URI root.
| @Bean
| public ServletRegistrationBean<Servlet> getRootServlet(RootResources rootResources) {
| return new ServletRegistrationBean<>(rootResources, "/*");
| }
|
| // Injected child resource.
| @Bean
| public HelloWorldResource getHelloWorldResource() {
| return new HelloWorldResource();
| }
|
| // Injected child bean used in injected child resource.
| @Bean
| public HelloWorldMessageProvider getHelloWorldMessageProvider() {
| return new HelloWorldMessageProvider("Hello Spring injection user!");
| }
| }
Our root resource servlet serves as a router page. It is defined as follows:
| @Rest(
| title="Root resources",
| description="Example of a router resource page.",
| children={
| HelloWorldResource.class,
| DtoExamples.class,
| UtilityBeansResource.class,
| HtmlBeansResource.class,
| ConfigResource.class,
| ShutdownResource.class
| }
| )
| @HtmlDocConfig(
| widgets={
| ContentTypeMenuItem.class
| },
| navlinks={
| "api: servlet:/api",
| "stats: servlet:/stats",
| "$W{ContentTypeMenuItem}",
| "source: $C{Source/gitHub}/org/apache/juneau/examples/rest/$R{servletClassSimple}.java"
| },
| aside={
| "<div 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'>API</span> link provided that lets you see the generated swagger doc for this page.</p>",
| " <p>Also note the <span class='link'>STATS</span> link that provides basic usage statistics.</p>",
| " <p>Also note the <span class='link'>SOURCE</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>"
| },
| asideFloat="RIGHT"
| )
| @SerializerConfig(
| quoteChar="'"
| )
| public class RootResources extends BasicSpringRestServletGroup {
| private static final long serialVersionUID = 1L;
| }
The org.apache.juneau.examples.rest.springboot.HelloWorldResource class shows an example of a child resource
defined as an injected bean.
| @Rest(
| title="Hello World",
| description="An example of the simplest-possible resource",
| path="/helloWorld"
| )
| @HtmlDocConfig(
| aside={
| "<div style='max-width:400px' class='text'>",
| " <p>This page shows a resource that simply response with a 'Hello world!' message</p>",
| " <p>The POJO serialized is a simple String.</p>",
| "</div>"
| }
| )
| public class HelloWorldResource extends BasicRestObject {
|
| @Autowired
| private HelloWorldMessageProvider messageProvider;
|
| @RestGet(path="/*", summary="Responds with injected message")
| public String sayHello() {
| return messageProvider.get();
| }
| }
Note that the message rendered is coming from our injected message provider: