简体   繁体   中英

Spring servlet works on IntelliJ but doesn't work on tomcat

I have write a small application with SpringBoot and I have test it using IntelliJ (I have run the app from IntelliJ) and everything works fine; but when I have try to deploy the war on my local tomcat ( or on a tomcat inside a docker container ) I have receive error 404 NotFound (inside the tomcat log there aren't errors); I think that maybe there are some problems whit the startup of the application.This is my main class:

@SpringBootApplication
public class DockerProxyMain extends SpringBootServletInitializer {

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

I have only one controller:

@RestController
public class DockerProxyController {
...
}

An the pom

<packaging>war</packaging>

    <groupId>it.xxx</groupId>
    <artifactId>dockerProxy</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
    </parent>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.6</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>${artifactId}</finalName>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

I haven't any web.xml inside the project. Any suggestions? Thanks.

Add below code in main class:

@Override   
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {    
   return application.sources(DockerProxyMain.class); 
}

I solved it: the problem was

<properties>
    <java.version>11</java.version>
</properties>

when I remove this part from the pom the spring app starts to respond correctly; I think that my tomcat instance hasn't that version of java. I still don't understand why there weren't any errors inside tomcat's logs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM