GAE端点例外:服务名称不匹配

时间:2017-11-26 07:38:37

标签: java google-app-engine objectify google-cloud-endpoints-v2

我尝试在Google App Engine上为我的项目设置REST API,并且已经部署了。但是,调用API会导致错误500返回给客户端,而在GAE的日志中,会显示以下堆栈跟踪。

堆栈追踪:

com.google.apphosting.runtime.jetty9.JettyLogger warn: /_ah/api/handler/v1/fetchlatest/all (JettyLogger.java:29)
java.lang.IllegalArgumentException: service name mismatch
    at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122)
    at com.google.api.control.aggregator.CheckRequestAggregator.check(CheckRequestAggregator.java:223)
    at com.google.api.control.Client.check(Client.java:177)
    at com.google.api.control.ControlFilter.doFilter(ControlFilter.java:249)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at com.google.api.control.ConfigFilter.doFilter(ConfigFilter.java:120)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at com.googlecode.objectify.ObjectifyFilter.doFilter(ObjectifyFilter.java:48)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at com.google.apphosting.runtime.jetty9.SaveSessionFilter.doFilter(SaveSessionFilter.java:37)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at com.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.java:60)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:48)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:582)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:524)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1180)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:512)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1112)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at com.google.apphosting.runtime.jetty9.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:297)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)
    at org.eclipse.jetty.server.Server.handle(Server.java:534)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:320)
    at com.google.apphosting.runtime.jetty9.RpcConnection.handle(RpcConnection.java:202)
    at com.google.apphosting.runtime.jetty9.RpcConnector.serviceRequest(RpcConnector.java:81)
    at com.google.apphosting.runtime.jetty9.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:108)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.dispatchServletRequest(JavaRuntime.java:680)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.dispatchRequest(JavaRuntime.java:642)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:612)
    at com.google.apphosting.runtime.JavaRuntime$NullSandboxRequestRunnable.run(JavaRuntime.java:806)
    at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:274)
    at java.lang.Thread.run(Thread.java:745)

我不确定问题是什么,但是从堆栈跟踪中猜测,是否可能是因为在web.xml中配置了Objectify的过滤器,因此,所有请求都是首先命中Objectify?虽然我不得不承认,这似乎不太可能,因为其他请求被路由到正确的servlet。

Objectify Filter:

<filter>
    <filter-name>ObjectifyFilter</filter-name>
    <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ObjectifyFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

2 个答案:

答案 0 :(得分:2)

似乎web.xml包含错误的init-param(endpoint.serviceName而不是endpoints.serviceName)。最重要的是,即使是物业的参数价值也是错误的。不可否认,我们花了比预期更长的时间来找出应该在同行评审中找到的东西。

答案 1 :(得分:2)

如果您遵循https://cloud.google.com/endpoints/docs/frameworks/java/adding-api-management上的官方文档,则会遇到此问题

有一个部分告诉您将一些代码复制到web.xml中,显示:

...
<init-param>
    <param-name>endpoints.serviceName</param-name>
    <param-value>${endpoints.project.id}.appspot.com</param-value>
</init-param>
...

目前尚不清楚该变量的解析方式。他们不会指示您将其更改为实际的项目ID,因此您认为它会被插件或其他内容更新...

但实际上不是。事实证明,他们正在从https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/appengine-java8/endpoints-v2-backend

的github项目中获取一些代码。

但是他们忽略了该变量实际上是由该项目的build.gradle文件中的自定义任务更新的。

// this replaces the ${endpoints.project.id} in appengine-web.xml and web.xml
task replaceProjectId(type: Copy) {
    from 'src/main/webapp/WEB-INF/'
    include '*.xml'
    into "build/exploded-${archivesBaseName}/WEB-INF"
    expand(endpoints:[project:[id:projectId]])
    filteringCharset = 'UTF-8'
}
assemble.dependsOn replaceProjectId

希望这会有所帮助!