Convert existing Spring MVC application to Spring Boot


Convert an existing Spring MVC application to Spring Boot

Until now I runned my web applications which I made with Spring MVC on a servlet container like Apache Tomcat. Sometimes, if you like to start the application on a new machine it’s hard to get them running. You have to download Tomcat, deploy the app, and so on… It would be nice to start them like normal Java applications. For this reason I like to run my sample application with Spring Boot. Spring Boot will use an embedded tomcat to start the app.

Here wo go…

Dependencies

My sample project is build with maven. First I have to add the “spring-boot-starter-web” dependency to my project:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>${spring-boot-version}</version>
</dependency>

For some reasons, in my case, spring was missing the aspectj-Library when starting the application with spring boot. So I also added it:

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.5</version>
</dependency>

Create executable application class

With the @SpringBootApplication Annotation you can tell spring boot, which class should be your executable application class. To start the application you have to use the method SpringApplication.run().

Here you see my example:

package ch.hsr.faith.application.rest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(
				"classpath:/META-INF/rest-services-config.xml", args);
	}

}

The first parameter of the run()-Method takes the path to my spring-config-xml, I already had before, as a string.

Remove traditional “webapp” folder

In my Spring MVC application I had a “webapp” folder with the web.xml file, spring configuration files, and other properties-Files. My new Spring boot app does not need the web.xml anymore, so I deleted it. The other config-files I moved to the src/main/resources/META-INF directory:

renamed:    src/main/webapp/WEB-INF/config/database.properties -> src/main/resources/META-INF/database.properties
renamed:    src/main/webapp/WEB-INF/config/repository-context.xml -> src/main/resources/META-INF/repository-context.xml
renamed:    src/main/webapp/WEB-INF/config/rest-services-config.xml -> src/main/resources/META-INF/rest-services-config.xml
renamed:    src/main/webapp/WEB-INF/config/security-config.xml -> src/main/resources/META-INF/security-config.xml
deleted:    src/main/webapp/WEB-INF/web.xml

Add maven plugin

To run my app with maven, I use the spring-boot-maven-plugin. So I added it to my POM-File:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot-version}</version>
        </plugin>
    </plugins>
</build>

Run the application

Now I’m able to run my spring boot application with the following command:

mvn spring-boot:run

And it works! That was very easy…

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.2.RELEASE)
...
2015-04-06 16:30:49.232  INFO 9051 --- [lication.main()] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2015-04-06 16:30:49.234  INFO 9051 --- [lication.main()] c.h.faith.application.rest.Application   : Started Application in 6.522 seconds (JVM running for 8.888)

Running app