Migrating From Spring Boot to Wildfly Swarm

DZone 's Guide to

Migrating From Spring Boot to Wildfly Swarm

Learn how to move from Spring Boot to Wildfly Swarm in this great tutorial, complete with the code snippets you'll need.

· Integration Zone ·
Free Resource

One of the more interesting features in Wildfly Swarm is support for Spring based applications. So what does it take to migrate a Spring Boot application to Swarm?

To answer this question we’ll take a Spring boot application written to show off Elide and the creation of a JSON-API compatible REST API. You can find the Spring Boot source code for this application on GitHub.

One of the issues you’ll find with these early versions of Swarm is that the dependencies are often not in sync with each other, and can only be found in specialized repositories.

In this, the Swarm Spring library is only at version 1.0.0-Alpha9-SNAPSHOT while most of the other Swarm libraries are at version 1.0.0-Beta2.

Also, I could only find the Swarm Spring library in a third party Maven repo at https://repository-projectodd.forge.cloudbees.com/snapshot

I expect these inconsistencies will be resolved as Swarm makes a final release, but for now be aware that some hunting is required to get the required dependencies.

Converting a Spring Boot application back to a regular WAR file was an interesting exercise. It made me appreciate just how much work has gone into the Spring Boot plugins to make things “just work.” The Swarm Spring library doesn’t actually have any dependencies on Spring, and so it is up to the developer to defines these in the build script manually. You are also forced to take care of all those little configuration items that Spring Boot implement by default.

The resulting build script is not complex, but it does include a lot of additional dependencies that Spring Boot normally deals with for you.

buildscript {
    repositories {

    dependencies {
        classpath "org.wildfly.swarm:wildfly-swarm-plugin:1.0.0.Beta2"

group 'com.matthewcasperson'
version '1.0-SNAPSHOT'

apply plugin: 'war'
apply plugin: 'wildfly-swarm'

sourceCompatibility = 1.8

repositories {
    maven {
        url 'http://repository.jboss.org/nexus/content/groups/public-jboss'
    maven {
        url 'https://maven.repository.redhat.com/nexus/content/repositories/public'
    maven {
        url 'https://repository-projectodd.forge.cloudbees.com/snapshot'

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile 'org.wildfly.swarm:bootstrap:1.0.0.Beta2'
    compile 'org.wildfly.swarm:spring:1.0.0.Alpha9-SNAPSHOT'

    compile 'javax.xml.bind:jaxb-api:2.2.12'
    compile 'org.apache.commons:commons-dbcp2:2.1.1'
    compile 'org.springframework:spring-orm:4.2.5.RELEASE'
    compile 'org.springframework:spring-core:4.2.5.RELEASE'
    compile 'org.springframework:spring-beans:4.2.5.RELEASE'
    compile 'org.springframework:spring-webmvc:4.2.5.RELEASE'
    compile 'org.springframework.security:spring-security-config:4.0.4.RELEASE'
    compile 'org.springframework.security:spring-security-web:4.0.4.RELEASE'
    compile 'org.springframework:spring-tx:4.2.5.RELEASE'
    compile 'org.springframework.data:spring-data-jpa:1.9.4.RELEASE'
    compile 'org.springframework.security:spring-security-core:4.0.4.RELEASE'

        These are the Elide dependencies
    compile 'com.yahoo.elide:elide-core:2.0.1'
        We then need to include Hibernate 5
    compile 'org.hibernate:hibernate-entitymanager:5.0.7.Final'
        We'll be using Hibernate 5, so we need the Elide Hibernate 5 datastore
    compile 'com.yahoo.elide:elide-datastore-hibernate5:2.0.1'
        This is so we can connect to the MySQL database
    compile 'mysql:mysql-connector-java:5.1.38'

We need a web.xml file to initialise Spring.

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

As well as a Spring XML configuration file to initialize a JPA EntityManagerFactory, a JpaTransactionManager, and a DataSource.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    <context:component-scan base-package="com.matthewcasperson.elidetest"/>

    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost/test"/>
        <property name="username" value="root"/>
        <property name="password" value="password"/>
        <property name="initialSize" value="3"/>

    <bean class="org.springframework.orm.jpa.JpaTransactionManager"
        <property name="entityManagerFactory" ref="entityManagerFactory" />

        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.matthewcasperson.elidetest.jpa" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        <property name="jpaProperties">
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>

Finally, we need a class extending AbstractSecurityWebApplicationInitializer in order to allow the Spring Security library to intercept requests and enforce authentication.

package com.matthewcasperson.elidetest;

import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;

 * Register the spring security filter
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {

With these changes implemented, we essentially have a stock standard Spring WAR application, and at this point it will just work with Swarm.

Once you deal with the various repos holding the required Swarm dependencies and reimplement all the features that Spring Boot deals with for you, the process of migrating from Spring Boot to Wildfly Swarm is pretty straight forward.

Grab the source code for this article at GitHub.

integration, java, spring, spring boot, wildfly

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}