gpt4 book ai didi

java - 在 Android Studio 中向现有 App Engine 后端服务器添加新端点

转载 作者:行者123 更新时间:2023-11-30 10:46:30 25 4
gpt4 key购买 nike

我在本地 Android Studio 中有一个 App Engine 后端和 Google Cloud Messaging 设置。

它具有如下原始端点:

  • 消息端点
  • 注册端点

我创建了一个新的 Java 文件并添加了一个名为 UserRegistration 的新类,如下所示:

package com.xxxxx.gcmbackend;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.api.server.spi.response.CollectionResponse;

import java.util.List;
import java.util.logging.Logger;
import javax.inject.Named;

import static com.xxxxxx.gcmbackend.OfyService.ofy;

@Api(
name = "register",
version = "v1",
namespace = @ApiNamespace(
ownerDomain = "gcmbackend.xxxxx.com",
ownerName = "gcmbackend.xxxxx.com",
packagePath=""
)
)


public class UserRegistrationEndpoint {
private static final Logger log = Logger.getLogger(RegistrationEndpoint.class.getName());
@ApiMethod(name = "register")
public void registerDevice(@Named("regId") String regId, @Named("username") String username, @Named("phone") String phone) {
if(findRecord(regId) != null) {
log.info("Device " + regId + " already registered, skipping register");
return;
}
RegistrationRecord record = new RegistrationRecord();
record.setRegId(regId);
record.setUsername(username);
record.setPhone(phone);
ofy().save().entity(record).now();
}
private RegistrationRecord findRecord(String regId) {
return ofy().load().type(RegistrationRecord.class).filter("regId", regId).first().now();
}
}

但是,部署后,我在 API 资源管理器中看不到此端点。如何使用 Google Cloud Messaging 将新的工作端点添加到 App Engine 后端?

最佳答案

通过检查现有的代码结构,我发现每个新的 API 都必须添加到 web.xml 文件中。我将我的新端点添加为:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<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>
<servlet>
<servlet-name>SystemServiceServlet</servlet-name>
<servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
<init-param>
<param-name>services</param-name>
<param-value>com.xxxxx.gcmbackend.RegistrationEndpoint, com.xxxxx.gcmbackend.MessagingEndpoint, com.xxxxx.gcmbackend.UserRegistrationEndpoint</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SystemServiceServlet</servlet-name>
<url-pattern>/_ah/spi/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

关于java - 在 Android Studio 中向现有 App Engine 后端服务器添加新端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36522616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com