首页 > 解决方案 > Google cloud App engine Spring deploy error

问题描述

Im trying to deploy a springboot rest api to google cloud's app engine, following this recent tutorial: https://medium.com/@smccartney09/deploy-a-spring-boot-api-to-gcp-app-engine-722198bab4d4and

However i'm getting this error:

org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

The app runs normally in localhost, from what I can see trying to debug, on localhost springboot launches a tomcat web server, and it doesn't in the logs form google app run deployment. Does this mean I have to use google's web server instead of tomcat? i'm really lost as to what i'm doing wrong right now.

POM xml:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/>
    </parent>
    <groupId>com.owl</groupId>
    <artifactId>owl-server</artifactId>
    <version>0.0</version>
    <name>owl-server</name>
    <description>owl backend server</description>

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

    <dependencies>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.3.9</version>
        </dependency>
        <dependency>
            <groupId>io.vavr</groupId>
            <artifactId>vavr</artifactId>
            <version>0.10.3</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>


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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
            <version>1.2.8.RELEASE</version>
        </dependency>

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

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

main:

@SpringBootApplication
@EnableCaching
public class OwlServerApplication {

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

    }

Ok, so after a lot of reading i think it might be because google app engine doesnt support tomcat as a webserver, and instead only supports jetty? so i tried the following:

added to pom xml:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

but this does nothing, same exact error. I then tried to add jetty as well, which runs locally great as a jetty web server, but gives the same error on app engine. So we know the error is due to google app engine not supporting tomcat or jetty, but what do i use now? Anyone have any idea on whats going wrong?

标签: springspring-bootgoogle-app-enginegoogle-cloud-platform

解决方案


所以经过大量调试,这里的问题出在spring security。如果你有一个扩展 WebSecurityConfigurerAdapter 的类,spring 会自动尝试将它应用到嵌入式 tomcat web 服务器。谷歌应用引擎不支持它,这打破了它。只需删除课程,该应用程序即可运行。


推荐阅读