带有App Engine的Spring Application使404或503在本地运行,但在部署到Appengine时工作正常

问题描述 投票:0回答:0

我有一个spring应用,已成功将其部署到云中的app引擎,但是在本地进行开发时遇到了困难。

当smartJ的AppEngine运行配置耗尽时,我得到了404。 带日志

WARNING: No file found for: /index

这让我认为intellij的本地应用程序引擎运行配置未正确附加servlet。

当在控制台中使用mvn appengine:run时,我得到503,页面显示503时没有日志行,这对于503来说并不是意外的(最后一个日志行状态服务器正在运行)。

在控制台中运行时,我确实注意到在启动日志中有:

[INFO] java.lang.NoClassDefFoundError: com/google/appengine/tools/util/Action
[...]
[INFO] Caused by: 
[INFO] java.lang.ClassNotFoundException: com.google.appengine.tools.util.Action

这可以解释控制台中的503。

之所以如此奇怪,是因为一切在我的开发和生产环境中都运行良好(我在我的域中的示例站点子目录下运行): https : //samplesite.taylormade.space/

这是我的pom:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>space.taylormade</groupId>
    <artifactId>samplesite</artifactId>
    <version>${appengineVersion}</version>
    <name>samplesite</name>
    <description>Sample site</description>

    <!--
        for more information on using war packaging for a springboot application in order to get a app engine standard deploy go to:
        https://github.com/GoogleCloudPlatform/getting-started-java/tree/master/appengine-standard-java8/springboot-appengine-standard
    -->
    <packaging>war</packaging>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <appengineVersion>local</appengineVersion>
        <appengineProject></appengineProject>
    </properties>

    <dependencies>
        <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>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Exclude any jul-to-slf4j -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jul-to-slf4j</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.google.appengine</groupId>
            <artifactId>appengine-maven-plugin</artifactId>
            <version>1.9.71</version>
        </dependency>
        <!--For servlet attachment in app engine-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.google.appengine</groupId>
                <artifactId>appengine-maven-plugin</artifactId>
                <version>1.9.71</version>
                <configuration>
                    <appId>${appengineProject}</appId>
                    <version>${appengineVersion}</version>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我没有运行web.xml因为我在我的主类中扩展了SpringBootServletInitializer

package space.taylormade.samplesite;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;

@Configuration
@SpringBootApplication
public class SamplesiteApplication extends SpringBootServletInitializer {

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

}

这是我的appengine-web.xml:

    <?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <application>tigermaestro-1297</application><!-- unused for Cloud SDK based tooling -->
    <version>0.0.1</version><!-- unused for Cloud SDK based tooling -->
    <threadsafe>true</threadsafe>
    <runtime>java8</runtime>
    <system-properties>
        <property name="java.util.logging.config.file" value="WEB-INF/classes/logging.properties"/>
    </system-properties>
</appengine-web-app>

我有两个控制器:

package space.taylormade.samplesite;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
public class IndexController {
  @RequestMapping("")
  public String root(){return index();}

  @RequestMapping("index")
  public String index(){return "index";}
}

并且(为了确保组件扫描得到一切):

package space.taylormade.samplesite.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/hello-world")
public class HelloWorldController {
  @RequestMapping("")
  public String helloWorld(){
    return "helloworld";
  }
}

如果有人对如何使其在App Engine中以与App Engine中相同的方式在本地运行有任何想法,那就太好了。 现在,我正在注释掉tomcat排除项,并从spring run config本地运行,然后在提交之前取消注释该排除项,这变得越来越老了。

java spring google-app-engine http-status-code-404 spring-web
© www.soinside.com 2019 - 2024. All rights reserved.