【问题标题】:WebServlet annotations are in use but GAE is trying to find web.xmlWebServlet 注释正在使用中,但 GAE 正在尝试查找 web.xml
【发布时间】:2018-09-07 13:35:23
【问题描述】:

AFAIK 如果我使用 WebServlet 注释,我不应该需要 web.xml。 我正在尝试使用 gradle 的 Google App Engine Standard 本地服务器,但尝试查找 web.xml 失败。

C:\Projects\x\x\Research\testeng>gradle appengineRun
2018 年 3 月 28 日 下午 1:17:18 java.util.prefs.WindowsPreferences
警告: 无法在根目录打开/创建首选项根节点 Software\JavaSoft\Prefs 0x80000002。 Windows RegCreateKeyEx(...) 返回错误代码 5。
三月 2018 年 2 月 28 日下午 1:17:18 com.google.apphosting.utils.config.AbstractConfigXmlReader readConfigXml
严重:收到异常处理 C:\Projects\x\x\Research\testeng\build\exploded-testeng\WEB-INF\web.xml
com.google.apphosting.utils.config.AppEngineConfigException: 不能 定位 C:\Projects\x\x\Research\testeng\build\exploded-testeng\WEB-INF\web.xml

我做错了什么...?

我得到了一个 servlet 的存根

@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}

我的 build.gradle 非常接近 Using Gradle and the App Engine Plugin 中使用的内容:

buildscript {    // Configuration for building
  repositories {
    jcenter()    // Bintray's repository - a fast Maven Central mirror & more
    mavenCentral()
  }
  dependencies {
    classpath 'com.google.cloud.tools:appengine-gradle-plugin:+'    // latest App Engine Gradle tasks
  }
}

repositories {   // repositories for Jar's you access in your code
  maven {
    url 'https://oss.sonatype.org/content/repositories/snapshots' // SNAPSHOT repository (if needed)
  }
  mavenCentral()
  jcenter()
}

apply plugin: 'java'                              // standard Java tasks
apply plugin: 'war'                               // standard Web Archive plugin
apply plugin: 'com.google.cloud.tools.appengine'  // App Engine tasks

sourceSets {
   // main.kotlin.srcDirs += "src/main/kotlin"
    main.java.srcDirs += "src/main/java"
}

dependencies {
  compile 'com.google.appengine:appengine-api-1.0-sdk:+'  // Latest App Engine Api's
  providedCompile 'javax.servlet:javax.servlet-api:3.1.0'

  compile 'jstl:jstl:1.2'

// Add your dependencies here.
//  compile 'com.google.cloud:google-cloud:+'   // Latest Cloud API's http://googlecloudplatform.github.io/google-cloud-java

  testCompile 'junit:junit:4.12'
  testCompile 'com.google.truth:truth:0.33'
  testCompile 'org.mockito:mockito-all:1.10.19'

  testCompile 'com.google.appengine:appengine-testing:+'
  testCompile 'com.google.appengine:appengine-api-stubs:+'
  testCompile 'com.google.appengine:appengine-tools-sdk:+'
}

// Always run unit tests
appengineDeploy.dependsOn test
appengineStage.dependsOn test

appengine {  // App Engine tasks configuration
  deploy {   // deploy configuration

  }
}

test {
  useJUnit()
  testLogging.showStandardStreams = true
  beforeTest { descriptor ->
     logger.lifecycle("test: " + descriptor + "  Running")
  }

  onOutput { descriptor, event ->
     logger.lifecycle("test: " + descriptor + ": " + event.message )
  }
  afterTest { descriptor, result ->
    logger.lifecycle("test: " + descriptor + ": " + result )
  }
}

group   = "com.testeng"        // Generated output GroupId
version = "1.0-SNAPSHOT"       // Version in generated output

sourceCompatibility = 1.8     // App Engine Flexible uses Java 8
targetCompatibility = 1.8     // App Engine Flexible uses Java 8

【问题讨论】:

    标签: java google-app-engine servlets google-cloud-platform build.gradle


    【解决方案1】:

    如果您在 Java 中使用 GAE 标准,则需要 web.xml 和 appengine-web.xml,因为它们用于描述您的应用程序的配置方式。

    您可以查看文档以了解有关您的应用程序所需的配置设置的详细信息。

    【讨论】:

    • 我有 appengine-web.xml 但它没有指定运行时版本。在我在其中添加 java8 作为运行时后,它现在可以工作了。
    【解决方案2】:

    解决了。

    我使用的 appengine-web.xml 与 appengine-web.xml Reference 中的示例相同。 添加了运行时元素并删除了未使用的东西,它可以工作。这是现在的样子:

    <?xml version="1.0" encoding="utf-8"?>
    <appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
      <runtime>java8</runtime>
      <threadsafe>true</threadsafe>
    </appengine-web-app>
    

    【讨论】:

      【解决方案3】:

      这不是答案,而是在其他人面临类似情况时节省时间的提示。

      我有一些 servlet 和一些 WebServlet 注释的 servlet 的 web.xml 条目。在这种情况下,带注释的 servlet 不会在 GAE Java 标准环境中注册为处理程序。本地没有问题。

      【讨论】:

        猜你喜欢
        • 2020-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-04
        • 1970-01-01
        • 2011-11-13
        • 2011-09-26
        • 2013-08-14
        相关资源
        最近更新 更多