访问Hello-World Google Cloud Endpoint服务的URL是什么?

时间:2013-02-04 09:12:03

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

我已使用this blog post中所述的Generate AppEngine BackEnd在Eclipse中生成了Google Endpoint AppEngine项目。然而,该帖子所描述的内容,以及官方Google Docs描述不佳的内容,我可以使用本地访问该服务的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的Exerpt:

@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>

2 个答案:

答案 0 :(得分:7)

API请求路径通常应符合以下条件:

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

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

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

(当您提出list请求时会映射到GET

通常,/_ah/_api/explorer提供的API资源管理器可以轻松发现和查询这些网址。

答案 1 :(得分:1)

您可以使用以下方式控制路径:

  @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