我创建了一个简单的类 GenericEntity
并使用Google的工具生成支持的Cloud Endpoint .
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class GenericEntity {
/* Define the UniqueID as persistent and unique. Allow JDO to assign the value of UniqueId when uploaded with a null id. */
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long mUniqueId; // Value must be 'Long' and not a primitive 'long'!
@Persistent
private String mData;
[...]
我还可以使用API Explorer在 GenericEntityEndpoint
中成功创建和删除 GenericEntity
的实例 .
请求:
POST
Content-Type: application/json
X-JavaScript-User-Agent: Google APIs Explorer
{
"data": "hello SO!"
}
响应:
200 OK
问题是我想从本地桌面应用程序控制这些 endpoints ,但我对如何做到这一点感到困惑 . 我想 PersistenceManagerFactory
本地只有Google 's servers, and subsequently can'才能直接通过我的应用程序访问 . 是否存在我遗漏的最后一步,它将允许通过网络与这些生成的 endpoints 进行高级别交互,还是需要使用 HttpUrlRequest
函数实现我自己的服务器接口?
1 回答
Cloud endpoints 使用REST或RPC over HTTP发送JSON数据,并使用OAuth2管理身份验证,因此您可以从头开始编写桌面应用程序,也可以使用各种Google库连接到 endpoints . 对于Java应用程序,我通常使用您可以使用endpoint.sh生成的Android Cloud endpoints 库,我使用为Android生成的类从任何其他Java应用程序调用 Cloud endpoints :
更多细节(https://cloud.google.com/appengine/docs/java/endpoints/endpoints_tool)
一旦源jar由endpoint.sh生成,我通常会将其扩展到我的客户端Java项目中 . 否则,您可以编译生成的类并将它们捆绑为二进制jar . 完成此操作后,您需要处理身份验证部分 . 这可以通过使用OAuth2 Java客户端库(https://developers.google.com/api-client-library/java/apis/oauth2/v1)来完成,请参阅提供的示例 oauth2-cmdline-sample
一旦您的OAuth2令牌调用 Cloud endpoints 形式,您的桌面应用程序与调用任何其他Google API没有什么不同,服务对象和构建器模式都是相同的,例如根据 endpoints API的名称,您可以从endpoint.sh生成的类创建服务对象,如下所示