2013-02-04 42 views
4

我已經使用Generate AppEngine BackEnd在Eclipse中生成了Google Endpoint AppEngine項目,如this blog post中所述。然而,那篇文章並非所描述的,官方Google Docs也描述得不好,哪個URL可以在本地訪問該服務?訪問Hello-World 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(。 ..)

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

回答

8

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資源管理器可以很容易地發現和查詢這些URL。

+2

謝謝,這個答案幫了我 在途中。我花了一天的時間深入研究方法命名約定,最終撰寫了一篇博客文章,對此進行了總結:http://www.nilzorblog.com/2013/02/a-google-cloud-endpoints-hello- world.html – Nilzor

+1

只是爲了給我一個具體的答案 - 正確的URL是http:// localhost:8888/_ah/api/deviceinfoendpoint/v1/deviceinfo' – Nilzor

1

您可以通過使用CONTROLE路徑:

@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

相關問題