Skip to content

spring-mvc is not working properly with org.apache.tomcat.embed by jar package #34984

Closed as not planned
@myfaverate

Description

@myfaverate

Source Code

https://github.com/myfaverate/KotlinWeb/tree/master/ssmLearn/ssm-mvc

Question

1. application

.\gradlew run

normal!

2. war

.\gradlew war

Drag the war package to the tomcat webapps directory.

Notice Application.kt:

internal fun main() {
    val port = 8080
    // if tomcat server war
    val contextPath = "/ssm-mvc" // <-
    // if jar
    // val contextPath = ""
   ...
}

normal!

3. jar

This is a standard Java Web MVC project using embedded Tomcat. The main startup class Application.kt is as follows:

package edu.tyut

import org.apache.catalina.connector.Connector
import org.apache.catalina.startup.Tomcat
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File

private val logger: Logger = LoggerFactory.getLogger("Application")

internal fun main() {

    val port = 8080
    // if tomcat server war
    // val contextPath = "/ssm-mvc"
    // if jar
    val contextPath = ""
    val webAppDir = File("src/main/webapp")
    val tomcatBaseDir = "build/tomcat"

    val tomcat = Tomcat()
    tomcat.setPort(port)
    tomcat.setBaseDir(tomcatBaseDir)

    logger.info("Starting tomcat on Port: {}", port)
    logger.info("Starting tomcat on contextPath: {}", contextPath)
    logger.info("Starting tomcat on webAppDir: {}", webAppDir.absolutePath)
    logger.info("Starting tomcat on tomcatBaseDir: {}", tomcatBaseDir)

    tomcat.addWebapp(contextPath, webAppDir.absolutePath)

    tomcat.start()
    val connector: Connector = tomcat.connector
    logger.info("Starting success connector: $connector ...")
    tomcat.server.await()
}
The script for packaging into a JAR is as follows:

tasks.jar {
    archiveFileName.set("${project.name}.jar")
    manifest {
        attributes["Main-Class"] = "edu.tyut.ApplicationKt"
    }
    from(configurations.runtimeClasspath.get().map { zipTree(it) }) {
        exclude("META-INF/*.RSA", "META-INF/*.SF", "META-INF/*.DSA")
    }
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

packaging and run

.\gradlew jar
java -jar ssm-mvc.jar

Unfortunately, the embedded Tomcat failed to load the WebApplicationInitializer implementation (specifically edu.tyut.config.WebInitializer), resulting in only the startup logs from Application.kt being visible.

The jar package doesn't work properly

Access the URL http://localhost:8080/hello in a browser, is 404

Expectation

The jar package works normally. Access the URL http://localhost:8080/hello in a browser, is 200.

spring-mvc tomcat-embed kotlin

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: stackoverflowA question that's better suited to stackoverflow.comstatus: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions