【问题标题】:What is the URL to access a Hello-World Google Cloud Endpoint service?访问 Hello-World Google Cloud Endpoint 服务的 URL 是什么?
【发布时间】:2013-02-04 09:12:02
【问题描述】:

我使用Generate AppEngine BackEnd 在Eclipse 中生成了一个Google Endpoint AppEngine 项目,如this blog post 中所述。但是,该帖子 没有 描述了什么,官方的 Google 文档描述也很糟糕,我可以在本地访问该服务的 URL 是什么?

生成的服务有一个生成的端点,称为 DeviceInfoEndpoint。代码如下所示,以及 web.xml 中的代码。鉴于我在本地端口 8888 上托管,我应该使用哪个 URL 访问 listDeviceInfo()?我尝试了以下方法:

  • http://localhost:8888/_ah/api/deviceinfoendpoint/v1/listDeviceInfo => 404
  • http://localhost:8888/_ah/spi/deviceinfoendpoint/v1/listDeviceInfo => 405 GET 不支持
  • http://localhost:8888/_ah/spi/deviceinfoendpoint/v1/DeviceInfo => 405 GET (...)
  • http://localhost:8888/_ah/spi/v1/deviceinfoendpoint/listDeviceInfo = > 405 GET(...)

DeviceInfoEndpoint.java 的摘录:

@Api(name = "deviceinfoendpoint")
public class DeviceInfoEndpoint {

/**
 * This method lists all the entities inserted in datastore.
 * It uses HTTP GET method.
 *
 * @return List of all entities persisted.
 */
@SuppressWarnings({ "cast", "unchecked" })
public List<DeviceInfo> listDeviceInfo() {
    EntityManager mgr = getEntityManager();
    List<DeviceInfo> result = new ArrayList<DeviceInfo>();
    try {
        Query query = mgr
                .createQuery("select from DeviceInfo as DeviceInfo");
        for (Object obj : (List<Object>) query.getResultList()) {
            result.add(((DeviceInfo) obj));
        }
    } finally {
        mgr.close();
    }
    return result;
}
}

Web.xml:

<?xml version="1.0" encoding="utf-8" standalone="no"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <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.example.dummyandroidapp.DeviceInfoEndpoint</param-value>
  </init-param>
 </servlet>
 <servlet-mapping>
  <servlet-name>SystemServiceServlet</servlet-name>
  <url-pattern>/_ah/spi/*</url-pattern>
 </servlet-mapping>
</web-app>

【问题讨论】:

    标签: google-app-engine google-cloud-endpoints


    【解决方案1】:

    API 请求路径一般应符合以下要求:

    http(s)://{API_HOST}:{PORT}/_ah/api/{API_NAME}/{VERSION}/
    

    如果您有兴趣获取/更新/删除特定资源,请在末尾添加一个 ID。在您的示例中,这表明您应该查询:

    http://localhost:8888/_ah/api/deviceinfoendpoint/v1/
    

    (当您发出 GET 请求时,映射到 list)。

    一般来说,/_ah/_api/explorer 提供的 API Explorer 可以轻松发现和查询这些 URL。

    【讨论】:

    • 谢谢,这个答案帮助了我。我花了一天时间研究方法命名约定,最后写了一篇博客文章,总结了这一点以及更多内容:nilzorblog.com/2013/02/…
    • 只是为了给我的案例一个具体的答案 - 正确的 URL 是 http://localhost:8888/_ah/api/deviceinfoendpoint/v1/deviceinfo
    【解决方案2】:

    您可以通过使用来控制路径:

      @ApiMethod(path="listDeviceInfo",  httpMethod = HttpMethod.GET)
        public List<DeviceInfo> listDeviceInfo(){
        //... definition
    
      }
    

    然后你可以从你的客户那里调用它: http://localhost:8888/_ah/api/deviceinfoendpoint/v1/listDeviceInfo

    如果你喜欢发送参数,那么:

    @ApiMethod(path="listDeviceInfo",  httpMethod = HttpMethod.GET)
            public List<DeviceInfo> listDeviceInfo(@Named("info") String info){
            //... definition
    
      }
    

    http://localhost:8888/_ah/api/deviceinfoendpoint/v1/listDeviceInfo?info=holamundo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-25
      • 2011-09-12
      • 1970-01-01
      • 2011-09-09
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多