App Engine。 HTTP错误:Gradle设置上的503

时间:2014-11-12 14:23:31

标签: java google-app-engine intellij-idea gradle build.gradle

我已在IntelliJ中设置了一个新的App Engine项目并将其Gradlefied。一切正常,直到我尝试添加HttpServlet。我在web.xml文件中添加了一个简单的测试类和关联的映射。当我去运行开发服务器时,我在启动时得到几个例外,然后是503错误。

我猜测它无法找到我的课程是根本原因,但我不明白为什么它无法找到我的课程。这与 javax.servlet.UnavailableException 有关吗?错误按以下顺序显示:

WARNING: EXCEPTION
java.lang.ClassNotFoundException:main.webapp.Test2

...

SEVERE: javax.servlet.ServletContext log: unavailable
javax.servlet.UnavailableException: main.webapp.Test2

...

WARNING: failed Test: java.lang.NullPointerException
Nov 12, 2014 2:07:56 PM com.google.apphosting.utils.jetty.JettyLogger warn
WARNING: Failed startup of context com.google.appengine.tools.development.DevAppEngineWebAppContext@34c8daa0{/,D:\Projects\WordBuzzIntegrated\build\classes\artifacts\app_engine_war_exploded}
java.lang.NullPointerException

测试类

package main.webapp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Test2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("BOO!");
    }
}

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
        <servlet-name>Test</servlet-name>
        <servlet-class>main.webapp.Test2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Test</servlet-name>
        <url-pattern>/Test</url-pattern>
    </servlet-mapping>

</web-app>

Root build.gradle

....
project(":app-engine") {
    apply plugin: 'java'
    apply plugin: 'war'
    apply plugin: 'appengine'
}
....

模块build.gradle

appengine {
    downloadSdk = true
    httpPort = 8080

    appcfg {
        email = 'xxxxxxxx@xxxx.xxx'
        passIn = true

        logs {
            severity = 1
            outputFile = file('mylogs.txt')
        }

        app {
            id = 'wordbuzzweb'
        }
    }

    sourceCompatibility = 1.7
    targetCompatibility = 1.7

    dependencies {
        appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.15'
        providedCompile 'javax.servlet:servlet-api:2.5'
        testCompile group: 'junit', name: 'junit', version: '4.11'
    }
}

1 个答案:

答案 0 :(得分:2)

我最终跟踪了这一点。我需要在build.gradle文件中指定源根目录,如下所示:

sourceSets {
    main {
        java.srcDirs = ['src']
    }
}

如果您通过添加

进行单元测试,也可以指定测试文件夹
        test.java.srcDirs = ['test']

所以我模块的最终build.gradle文件如下所示:

appengine {
    downloadSdk = true
    httpPort = 8080

    sourceSets {
        main {
            java.srcDirs = ['src']
            test.java.srcDirs = ['test']
        }
    }

    appcfg {
        email = 'xxxx.xxxxxx@xxxxx.com'
        passIn = true

        logs {
            severity = 1
            outputFile = file('mylogs.txt')
        }

        app {
            id = 'wordbuzzweb'
        }
    }

    dependencies {
        compile project(":comms")

        appengineSdk "com.google.appengine:appengine-java-sdk:$appEngineVersion"
        compile "com.google.appengine:appengine-api-1.0-sdk:$appEngineVersion"
        compile "com.google.appengine:appengine-endpoints:$appEngineVersion"
        compile "com.google.appengine:appengine-endpoints-deps:$appEngineVersion"

        compile "com.googlecode.objectify:objectify:5.1.1"
        compile group: "com.google.guava", name: "guava", version: "18.0"

        testCompile group: "junit", name: "junit", version: "4.11"
        testCompile "com.google.appengine:appengine-testing:$appEngineVersion"
        testCompile "com.google.appengine:appengine-api-labs:$appEngineVersion"
        testCompile "com.google.appengine:appengine-api-stubs:$appEngineVersion"

        compile "javax.servlet:servlet-api:2.5"
    }
}

您还需要在Gradle脚本中的某处添加以下内容,以指定您的应用引擎版本。

ext.appEngineVersion = '1.9.15'