In one of our recent projects we had the requirement to develop a series of REST services using a technology at our choice. We chose Spring Boot because of its simplicity and as a result we had to come up with a deployable artefact on the customers platform. In our case the platform was Oracle WebLogic Server 12c, although most of the times Spring Boot applications are deployed as standalone jars, maybe even in Docker containers. As this option was out of the question we went on searching a way to deploy the spring app on Weblogic. As it turned out, it isn’t difficult at all, provided one is aware of a couple of caveats.

  • Firstly, go to the spring boot initializr project (start.spring.io) and start choosing the desired libraries as well as the maven coordinates.
    • The single mandatory dependency for our project is Web, as we’re going to create an web application
    • Actuator (for monitoring, optional)
    • Maybe also Hateoas, if you need such features

Oracle WebLogic Server_01

  • After you download the generated archive go on and import the generated project in an IDE at your choice. Then edit the already generated Application class and make sure to extend SpringBootServletInitializer, to implement the WebApplicationInitializer interface and to override the configure method as it follows:

Before:

@SpringBootApplication
public class Application {

   public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
   }
}

After:

@ComponentScan   
@SpringBootApplication
public class Application extends SpringBootServletInitializer implements WebApplicationInitializer {

   public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
   }

   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
      return builder.sources(Application.class);
   }
}
  • As we want do deploy our application as a web application on the Weblogic server go to your pom.xml file and change the default packaging from jar to war:
<groupId>de.virtual7</groupId>
<artifactId>demo.wls</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>demo.wls</name>
<description>Demo project for Spring Boot</description>

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.3.5.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <java.version>1.8</java.version>
</properties>

<dependencies>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
   </dependency>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-hateoas</artifactId>
   </dependency>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
   </dependency>
</dependencies>
  • Edit also the build section of your pom.xml and override the maven-war-plugin, configuring it not to fail if the web.xml is missing:
<build>
…
   <plugins>
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-war-plugin</artifactId>
         <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
            <archive>
               <manifest>
                  <addDefaultImplementationEntries>false</addDefaultImplementationEntries>
               </manifest>
            </archive>
         </configuration>
      </plugin>
   </plugins>
…
</build>
  • Add a weblogic.xml file specifying the context root and the packages which should have precedence: (make sure to add it in the src/main/webapp/WEB-INF folder)
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
        xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app
        http://xmlns.oracle.com/weblogic/weblogic-web-app/1.4/weblogic-web-app.xsd">
    <wls:context-root>/v7</wls:context-root>
    <wls:container-descriptor>
        <wls:prefer-application-packages>
            <wls:package-name>org.slf4j.*</wls:package-name>
            <wls:package-name>org.springframework.*</wls:package-name>
        </wls:prefer-application-packages>
    </wls:container-descriptor>
</wls:weblogic-web-app>
  • Add another almost empty xml file (in our case called dispatcherServlet-servlet.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
  • Implement a basic rest controller and don’t forget to add the @ComponentScan annotation on the Application class, to make sure Spring will pick up the REST resources you create, e.g.:
@RestController
@RequestMapping("/resource")
public class ResourceController {

    @RequestMapping(method = RequestMethod.GET)
    String readResource() {
        return "hello!";
    }
}
  • Optionally use the maven weblogic plugin in order to deploy the generated artifact on a weblogic instance at our choice, when called locally or even from Jenkins:
  • Open the command line and type mvn clean install in order to build the project and create the deployable file (which will be generated in the {project_root}/target folder), then go to your weblogic administration console and deploy the artefact
  • Go to your_server_url:port/v7/resource and you should see

Oracle WebLogic Server_02

  • Because you included the actuator dependency you could also issue an request to /v7/health, to see the health status of your app (you can check all available endpoint provided by the actuator here: http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html)

Oracle WebLogic Server_03

  • And that was pretty much it. Congratulations, you deployed your first spring boot application as a war on weblogic!