Skip to main content

Spring Boot Overview

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:

org.apache.juneau.rest.springbootBasicSpringRestServlet - Basic servletBasicSpringRestServletGroup - Basic servlet group

These classes are the equivalent to the BasicRestServlet and 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 <a href="/site/apidocs/org/apache/juneau/rest/springboot/SpringRestServlet.html" target="_blank">SpringRestServlet</a> to allow use of injection.
// All REST objects are attached to this bean using the <a href="/site/apidocs/org/apache/juneau/rest/annotation/Rest.html#children()" target="_blank">Rest.children()</a> annotation.
@Bean
public RootResources getRootResources() {
return new RootResources();
}

// Registers our REST bean at the URI root.
@Bean
public ServletRegistrationBean 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
}
)
public class RootResources extends BasicSpringRestServletGroup {
private static final long serialVersionUID = 1L;
}

HTML representation

JSON representation

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={
"",
" This page shows a resource that simply response with a 'Hello world!' message",
""
}
)
@Component
public class HelloWorldResource extends BasicSpringRestServlet {

@RestGet
public String sayHello() {
return "Hello world!";
}
}

The POJO serialized is a simple String.

Note that the message rendered is coming from our injected message provider:

@Autowired 
private HelloWorldMessageProvider messageProvider;

@RestGet(path="/*", summary="Responds with injected message")
public String sayHello() {
return messageProvider.get();
}

HTML representation