我已經使用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
=> 404http://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>
謝謝,這個答案幫了我 在途中。我花了一天的時間深入研究方法命名約定,最終撰寫了一篇博客文章,對此進行了總結:http://www.nilzorblog.com/2013/02/a-google-cloud-endpoints-hello- world.html – Nilzor
只是爲了給我一個具體的答案 - 正確的URL是http:// localhost:8888/_ah/api/deviceinfoendpoint/v1/deviceinfo' – Nilzor