Introducing Spring Boot to Payara Micro! Read more in the article written by our newest Team Member Mert Caliskan - Java Champion, Software Stylist and Hacettepe University lecturer.
Spring Boot is an application-bootstrapping framework that helps with the creation of Spring based applications with no necessary XML configuration or code generation. It eases the configuration with its starter pom definitions and bootstrap annotations.
Project Creation and Dependency Setup
First things first, let’s start with creating our project infrastructure. We’ll choose maven for it, since Spring Boot works well with it. You can either create a maven-ized empty web application via your favorite IDE, or you can execute the archetype command as follows. Archetypes are maven projects that also create maven-based projects.
create -DgroupId=fish.payara.appserver -DartifactId=payara-micro-with-spring-boot-rest -Dversion=1.0 -DarchetypeArtifactId=maven-archetype-webapp
Next step is to add the dependencies. Spring Boot dependencies can either be integrated to a maven project through a parent pom definition or with a dependency management definition. Assuming that your project has its own maven parent project definition in its pom.xml, we added a dependency management definition for all the Spring Boot dependencies. Keep in mind that this does not add any dependencies directly to your project but allows you to select the ones you want via the
<dependency>tag. And you don’t need to define
<version>for each of those dependencies since it’s defined in the dependency management configuration already. The aforementioned definition is as follows:
spring-boot-starter-webmodule dependency, which is a starter pom 'that bootstraps you for doing web application development with Spring. With Spring Boot’s starter pom, there is no need to explicitly deal with dependencies of Spring. For
starter-web, dependencies like
spring-webmvcautomatically added to your project.
spring-boot-starter-tomcattransitive dependency since we will deploy our war module directly to Payara Micro, which already contains resources for Java EE. Of course the exclusion of this dependency will lead to compilation errors, so Servlet API dependency needs to be added with provided scope as given follows:
maven-compiler-plugindefinition with 1.8 compiler arguments. Keep in mind that maven compiles source code with Java 5 level by default.
Bootstrapping the REST web service
First we need to implement a main configuration class annotated with the
@SpringBootApplicationand it will be responsible for configuring and starting our application.
@SpringBootApplicationis a composing annotation provided by Spring Boot and it bundles
@ComponentScanannotations together in one place. The main class named
Applicationis given below.
SpringBootServletInitializerclass and overrides the
configuremethod. This is needed since we want to deploy our application as a war archive onto Payara Micro by following the traditional approach, instead of using embedded servers of the Spring Boot. The implementation of the REST web service is based on Spring MVC module and with the help of Spring Boot, we didn’t deal with any of its configuration like
DispatcherServletdefinition etc. The controller annotated with
@RestControllerdefines a person map and initializes it with 3 persons. The
getAll()method just responses back with all the persons.
Wrapping it up
The web.xml, web application deployment descriptor file, should be placed under WEB-INF folder in order to package the application as a war file first. Also the file is needed to correctly deploy our application onto Payara Micro. An empty Java EE7 compliant web.xml definition (note that it’s version 3.1, which aligns with Servlet 3.1 of Java EE7) is given below.
mvn clean install command within the application folder should create a war archive out of it.
Getting it up and Running
With Payara Micro it’s very easy to deploy a war archive through the console. Download Payara Micro 4.1.153 from here first, and then just execute the command given below.
java -jar payara-micro-4.1.153.jar --deploy %PATH_TO%/payara-micro-with-spring-boot-rest-1.0.war
You should be seeing an output similar to this:
By requesting for the URL, you will be getting a JSON output as follows: