我正在尝试使用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;
}
}
答案 0 :(得分:2)
您想要实现的是多类API。正如您可以阅读文档[1]:“多类API中类的@Api属性的任何差异都会导致”模糊“的API配置,这在端点中无效。”
因此,你可以使用注释继承来分割API与Java继承或@ApiReference继承。详细信息见[1]。
另一种选择是使用不同的后端版本,如[2]所述。
[1] https://cloud.google.com/appengine/docs/java/endpoints/multiclass