Creating Spring Boot Application for WebLogic and Tomcat

In this post, we are going to create our first Spring Boot Application. Since Tomcat is an embedded container that comes with Spring Boot application, it is easy to deploy in Tomcat container, but, today we will deploy our first Spring Boot Application in Oracle’s WebLogic server.

If you are new to WebLogic server, please download the latest version of WebLogic server first from here. I’m not going to discuss the installation process of WebLogic. Please follow the Oracle’s documentation on how to install and configure WebLogic in your local system.

Let start deploying our first Spring Boot Application in WebLogic. But, before that, lets create one sample application in Spring Boot. There are various ways of creating applications for Spring Boot. I recommend to start from here. For this first project, I will go step by step creating an application for Spring Boot. Please use the following details while creating your own project:

Download this project and import it to your best IDE. I’ll be using Intellij IDE for this tutorial.

After importing this project to your IDE, you will see two classes under …*.api package:
a. BootUserManagementApi.java (Main Spring Boot Application)
b. ServletInitializer.java

To deploy your app in WebLogic, you need to edit you ServletInitializer.java. Update this class as follows:

public class ServletInitializer extends SpringBootServletInitializer implements WebApplicationInitializer {
   @Override   
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
      return application.sources(BootUserMgmtApplication.class);   }
}

To deploy your application in WebLogic you need to implement WebApplicationInitializer in your ServletInitializer, which is the extra effort you need to perform.
Remember, we need to add this interface if you are going to deploy in WebLogic server. For Tomcat, its not necessary.
Next, you need to add a weblogic.xml file in your project to tell WebLogic Server to load this file while deploying you app. You need to add this file into ../src/main/web-app/WEB-INF folder. You need to create a folder for this. Code for weblogic.xml is as follows:

Now, create one package under you main package and name it as “controller” and add one class for the controller. I’m going to name this class as HomeController which is going to be a RestController for this project. Finally, your project structure should be like this:

application.properties

That’s it. You can now run your application. Go to http://localhost:8080/app/usermanagement/api/v1/home

You should see: Hello World

Remember one more thing, if you are going to deploy your app in WebLogic then you need to exclude your Tomcat from Gradle or Maven. Below will be my build.gradle script:

buildscript {
   ext {
      springBootVersion = '1.5.9.RELEASE'   
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
      classpath("javax.servlet:javax.servlet-api:4.0.0")
   }
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

group = 'user-management'
version = '1'
sourceCompatibility = 1.8

repositories {
   mavenCentral()
}

configurations {
   providedRuntime
}

dependencies {
   compile('org.springframework.boot:spring-boot-starter-web')
         {
            exclude module: "spring-boot-starter-tomcat"         
         }
   runtime('org.springframework.boot:spring-boot-devtools')
   compileOnly('org.projectlombok:lombok')
   testCompile('org.springframework.boot:spring-boot-starter-test')

   providedCompile 'javax.servlet:javax.servlet-api:4.0.0'
}

If you want to deploy your app in Tomcat then use following build.gradle:

buildscript {
   ext {
      springBootVersion = '1.5.9.RELEASE'   
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
//    classpath("javax.servlet:javax.servlet-api:4.0.0")   
   }
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

group = 'user-management'
version = '1'
sourceCompatibility = 1.8

repositories {
   mavenCentral()
}

configurations {
   providedRuntime
}

dependencies {
   compile('org.springframework.boot:spring-boot-starter-web')
//       {          
//              exclude module: "spring-boot-starter-tomcat"       
//       }   
   runtime('org.springframework.boot:spring-boot-devtools')
   compileOnly('org.projectlombok:lombok')
   testCompile('org.springframework.boot:spring-boot-starter-test')

// providedCompile 'javax.servlet:javax.servlet-api:4.0.0'
}

That’s all you need to do. If you have any questions or issues, please feel free to comment. I will help to fix your issues.

Complete project is available in Github.

Integration Testing Using Cucumber-Java
Preventing Mutation on Cached Object: EhCache