Hi,

I have a legacy java application which is a monolith web based on servlet. We want to develop new APIs but outside the monolith. But dependencies with the monolith is a problem. Hence we are exploring to use Java9 modules in the legacy app. As a first step, we just make the whole monolith under a module by folder refactoring and add a module-info.java which exports certain packages. Then another folder in the project will serve as API module which is based on Spring Boot. Time being it is a simple application with a RestController and a get mapping.

My module-info for the API module is as follows.

module api_project {
    requires spring.web;  
    requires spring.boot;  
    requires spring.boot.starter.web;  
    requires spring.boot.autoconfigure;  
    opens net.ifao.api to spring.core;  
}

and the gradle dependecies for the API module is as follows

dependencies {  
    implementation project(':ibetms_project')  
    implementation('org.springframework.boot:spring-boot-starter-web') {  
        exclude group: 'org.apache.tomcat.embed', module: 'tomcat-embed-core'  
    }  
    compile "javax.servlet:javax.servlet-api:3.1.0"  
}

If you notice, I exclude the module tomcat-embed-core without which I get 100s of build errors such as

module spring.boot reads package javax.servlet from both javax.servlet.api and org.apache.tomcat.embed.core

this issue helped me about exclusion.

And you can see that I explicitly add javax.servlet-api.3.0.1. But even if I add this or not, I can build the project successfully, but during server start I get following error.

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

Is it expected? Should I add additional dependency? I created a sample application for a quick reference. Your help is appreciated?

Comment From: wilkinsona

Thanks for the sample. The main problem is that your api_project does not depend on an embedded container as you have excluded Tomcat from spring-boot-starter-web. You need to remove this exclusion to make Tomcat available. You then need to fix the problem with javax.servlet being available from two modules. The following diff makes both of these changes:

diff --git a/api_project/build.gradle b/api_project/build.gradle
index 10e33e8..5b467d3 100644
--- a/api_project/build.gradle
+++ b/api_project/build.gradle
@@ -15,13 +15,10 @@ repositories {

 dependencies {
     implementation project(':ibetms_project')
-    implementation('org.springframework.boot:spring-boot-starter-web') {
-        exclude group: 'org.apache.tomcat.embed', module: 'tomcat-embed-core'
-    }
+    implementation('org.springframework.boot:spring-boot-starter-web')
     testImplementation('org.springframework.boot:spring-boot-starter-test') {
         exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
     }
-    compile "javax.servlet:javax.servlet-api:3.1.0"
 }

 test {
diff --git a/api_project/src/main/java/net/ifao/api/ApiController.java b/api_project/src/main/java/net/ifao/api/ApiController.java
index def4de5..4cf78de 100644
--- a/api_project/src/main/java/net/ifao/api/ApiController.java
+++ b/api_project/src/main/java/net/ifao/api/ApiController.java
@@ -10,7 +10,7 @@ public class ApiController {
     @GetMapping("/user")
     User user() {
         User user = new User();
-        user.setName("winster");
+        user.setFirstName("winster");
         return user;
     }
 }
diff --git a/ibetms_project/build.gradle b/ibetms_project/build.gradle
index b0e1806..12b41f1 100644
--- a/ibetms_project/build.gradle
+++ b/ibetms_project/build.gradle
@@ -17,6 +17,6 @@ repositories {
 }

 dependencies {
-    providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
+    compileOnly 'javax.servlet:javax.servlet-api:3.1.0'
     testCompile 'junit:junit:4.12'
 }

It also fixes a compilation error when setting the user's name. With these changes in place, your application starts for me.

If you have any further questions, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.