Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Creating Spring Boot Application for WebLogic and Tomcat

DZone's Guide to

Creating Spring Boot Application for WebLogic and Tomcat

We look at some of the most popular frameworks for spinning up applications and servers. Read on to get started!

· Web Dev Zone ·
Free Resource

Access over 20 APIs and mobile SDKs, up to 250k transactions free with no credit card required

In this post, we are going to create our first Spring Boot Application. Since Tomcat is an embedded container that comes with Spring Boot applications, it is easy to deploy in Tomcat containers, but, today, we will deploy our first Spring Boot Application in Oracle’s WebLogic server. We will use Spring Boot 1.x here. I will create another post on how to deploy Spring Boot 2.x in WebLogic 12.1.2.1

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 Oracle’s documentation on how to install and configure WebLogic in your local system.

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

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

After importing this project to your IDE, you will see two classes under your root package:

  1. BootUserManagementApi.java (Main Spring Boot Application)

  2. 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 the 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:

Please note that we just need to include "org.slf4j.*", the rest can be excluded.

Now, create one package under your main package and name it “controller” and add one class for the controller. I’m going to name this class 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 Tomcat from Gradle or Maven. Below is 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 the 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.

The complete project is available in GitHub.

#1 for location developers in quality, price and choice, switch to HERE.

Topics:
spring boot application ,web dev ,spring boot tutorial ,tomcat tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}