在 Android Studio 中使用 Cloud Endpoints 创建两个或更多 API
Creating two or more APIs using Cloud Endpoints in Android Studio
我正在尝试在 Android Studio 中使用 Cloud Endpoints 模块创建两个 API,但是当我使用所有必需的注释和 运行 我的本地开发服务器时,它不会创建任何新的 API 但只有一个 API。是否有另一种方法可以创建多个 API。请帮助我为同一个后端模块创建多个 API。
这是我的第二个API
@Api(name = "myApi2", version = "v1")
public class LoginEndpoint
{
@ApiMethod(name = "storeData")
public MyBean storeData(@Named("eid")String eid, @Named("uname")String uname, @Named("password")String pass)
{
MyBean bean = new MyBean();
bean.dataStored = true;
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction();
try{
Key employeeKey = KeyFactory.createKey("Users", uname);
Entity user = new Entity(employeeKey);
user.setProperty("Username", uname);
user.setProperty("Email id", eid);
user.setProperty("Password", pass);
datastore.put(user);
txn.commit();
}finally {
if (txn.isActive())
{
txn.rollback();
bean.dataStored = false;
}
}
return bean;
}
}
这是我的第一个API
/**
* An endpoint class we are exposing
*/
@Api(name = "myApi", version = "v1")
public class MyEndpoint
{
/**
* A simple endpoint method that takes a name and says Hi back
*/
@ApiMethod(name = "sayHi")
public MyBean sayHi(@Named("name") String name) {
MyBean respon = new MyBean();
respon.setData("Hi," + name);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction();
try {
Key employeeKey = KeyFactory.createKey("Employee", "Joe");
Entity employee = new Entity(employeeKey);
employee.setProperty("vacationDays", 10);
datastore.put(employee);
txn.commit();
} finally {
if (txn.isActive()) {
txn.rollback();
}
}
return respon;
}
}
你想要实现的是一个MulticlassAPI。正如您在文档 [1] 中看到的:"Any difference in the @Api properties for classes in a multiclass API result in an "ambiguous" API 配置,在端点中不起作用。"
所以你可以使用注解继承来拆分你 API 与 Java 继承或 @ApiReference 继承。 [1].
中提供了详细信息
另一种选择是使用此处所述的不同后端版本 [2]。
[1] https://cloud.google.com/appengine/docs/java/endpoints/multiclass
我正在尝试在 Android Studio 中使用 Cloud Endpoints 模块创建两个 API,但是当我使用所有必需的注释和 运行 我的本地开发服务器时,它不会创建任何新的 API 但只有一个 API。是否有另一种方法可以创建多个 API。请帮助我为同一个后端模块创建多个 API。
这是我的第二个API
@Api(name = "myApi2", version = "v1")
public class LoginEndpoint
{
@ApiMethod(name = "storeData")
public MyBean storeData(@Named("eid")String eid, @Named("uname")String uname, @Named("password")String pass)
{
MyBean bean = new MyBean();
bean.dataStored = true;
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction();
try{
Key employeeKey = KeyFactory.createKey("Users", uname);
Entity user = new Entity(employeeKey);
user.setProperty("Username", uname);
user.setProperty("Email id", eid);
user.setProperty("Password", pass);
datastore.put(user);
txn.commit();
}finally {
if (txn.isActive())
{
txn.rollback();
bean.dataStored = false;
}
}
return bean;
}
}
这是我的第一个API
/**
* An endpoint class we are exposing
*/
@Api(name = "myApi", version = "v1")
public class MyEndpoint
{
/**
* A simple endpoint method that takes a name and says Hi back
*/
@ApiMethod(name = "sayHi")
public MyBean sayHi(@Named("name") String name) {
MyBean respon = new MyBean();
respon.setData("Hi," + name);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction();
try {
Key employeeKey = KeyFactory.createKey("Employee", "Joe");
Entity employee = new Entity(employeeKey);
employee.setProperty("vacationDays", 10);
datastore.put(employee);
txn.commit();
} finally {
if (txn.isActive()) {
txn.rollback();
}
}
return respon;
}
}
你想要实现的是一个MulticlassAPI。正如您在文档 [1] 中看到的:"Any difference in the @Api properties for classes in a multiclass API result in an "ambiguous" API 配置,在端点中不起作用。"
所以你可以使用注解继承来拆分你 API 与 Java 继承或 @ApiReference 继承。 [1].
中提供了详细信息另一种选择是使用此处所述的不同后端版本 [2]。
[1] https://cloud.google.com/appengine/docs/java/endpoints/multiclass