Java 示例客户端
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以下载我们的 Java 实时更新 API 客户端,以便开始实现实时更新。
前提条件:
创建服务账号,下载私钥。
开始使用:
- 下载 Java 客户端库,解压缩该文件以获取“mapsbooking”软件包。
-
在 IDE 中创建一个 Java 项目,然后从此代码库下载示例代码 InventoryUpdate.java 和 BookingNotification.java:
git clone https://maps-booking.googlesource.com/java-maps-booking-api-example
并将其导入到 src 目录下的 Java 项目中。
- 将私钥(JSON 文件)的副本复制到 src 目录。
- 将 Java 客户端库添加到项目的依赖项(包括 mapsbooking 和 libs-sources 目录下的 jar 文件)。
- 在两个 Java 文件中,按照 TODO 完成实现
如需详细了解 REST API,请点击此处。
问题排查:
-
尝试使用现有的 merchantId 或 serviceId 创建新的商家/服务时,您会收到以下错误消息:
409 Conflict
{
“code” : 409,
“errors” :
[{
“domain” : “global”,
“message” : “Requested entity already exists”,
“reason” : “alreadyExists”,
“debugInfo” : “detail: "[ORIGINAL ERROR] generic::already_exists: ...”\n"
}],
“message” : “Requested entity already exists”,
“status” : “ALREADY_EXISTS”
}
- 使用无效的 merchantId 或 serviceId 进行 replace availability 调用时,该调用不会抛出错误消息。这是因为通过 RTO 更改空闲时段支持无序执行,因此不会与商家 ID 或服务 ID 相关联。不过,由于 merchantId 或 serviceId 无效,请求中的槽位不会在生产环境中投放。如需提供这些空档,请通过 RTO 或 Feed 添加缺失的商家或服务。
- 在所有更新函数中,参数“updateMask”必须采用 FieldMask 格式,才能指定要更新的字段。API 必须仅更改掩码中指定的字段的值,而对其他字段保持不变。如果您传递的 updateMask 字符串有误,更新将不会生效。如果更新时未提供字段掩码,则该操作会应用于所有字段(就像指定了所有字段的字段掩码一样)。因此,如果您想更新所有字段,只需将 updateMask 设置为空即可。
- 响应中不会显示值为零的任何字段。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eDownload the Java real-time update API client and sample code to easily implement real-time updates for Google Maps Booking.\u003c/p\u003e\n"],["\u003cp\u003eBefore starting, ensure you have a service account created and your private key downloaded.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the provided troubleshooting section for common issues and solutions during implementation.\u003c/p\u003e\n"],["\u003cp\u003eRemember to utilize the \u003ccode\u003eupdateMask\u003c/code\u003e parameter in the update functions to specify the fields to be modified.\u003c/p\u003e\n"],["\u003cp\u003eDetailed information about the REST API and FieldMask format can be found through the provided links.\u003c/p\u003e\n"]]],["Download the Java real-time update API client and library. Create a Java project, import the sample code files, `InventoryUpdate.java` and `BookingNotification.java`, and copy your private key to the `src` directory. Add the Java Client Library to your project dependencies. Follow the TODOs in the Java files to complete implementation. Be aware of 409 errors with existing IDs. Replace availability calls won't throw errors with invalid IDs. Ensure the 'updateMask' parameter is in FieldMask format in update functions.\n"],null,["# Java Sample Client\n\nYou can download our\n[Java real time update API client](https://maps-booking.googlesource.com/java-maps-booking-api-example/) to help get started implementing\nthe real time updates.\n\nPrerequisite:\n-------------\n\nHave your service account created, download your private key.\n\nGet started:\n------------\n\n1. Download the [Java client library](https://dl.google.com/mapsbooking/apiclients/v1alpha/mapsbooking_java_public.zip) unzip the file to get the 'mapsbooking' package.\n2. Create a java project in your IDE, download the sample codes\n [InventoryUpdate.java](https://maps-booking.googlesource.com/java-maps-booking-api-example/+/master/InventoryUpdate.java) and\n [BookingNotification.java](https://maps-booking.googlesource.com/java-maps-booking-api-example/+/master/BookingNotification.java) from this repo:\n\n ```java\n git clone https://maps-booking.googlesource.com/java-maps-booking-api-example\n ```\n\n and import them into your java project under src directory.\n3. Make a copy of your private key (JSON file) to the src directory.\n4. Add the Java Client Library to the dependencies of your project (include jar files under mapsbooking and libs-sources directory).\n5. In both java files, follow the TODOs to complete your implementations\n\nMore detail about the REST API can be\n[found here](/maps-booking/reference/maps-booking-api/rest).\n\nTroubleshooting:\n----------------\n\n1. When trying to create a new merchant/service with an existing merchantId or\n serviceId, you will get the following error message:\n\n ```java\n 409 Conflict\n {\n \"code\" : 409,\n \"errors\" :\n [{\n \"domain\" : \"global\",\n \"message\" : \"Requested entity already exists\",\n \"reason\" : \"alreadyExists\",\n \"debugInfo\" : \"detail: \"[ORIGINAL ERROR] generic::already_exists: ...\"\\n\"\n }],\n \"message\" : \"Requested entity already exists\",\n \"status\" : \"ALREADY_EXISTS\"\n }\n ```\n2. When making a replace availaiblity call with an invalid merchantId or serviceId, the call will **not** throw an error message. This is because changes to availability slots via RTUs support out-of-order execution and thus is not associated with a merchantId or serviceId. However, the slots in the request will not be served in the production due to the invalid merchantId or serviceId. To make the slots servable, please add the missing merchant or service via RTUs or feeds.\n3. In all update functions, the parameter 'updateMask' must be in [FieldMask](/protocol-buffers/docs/reference/google.protobuf#fieldmask) format to specify the fields to update. The API is required to only change the values of the fields as specified in the mask and leave the others untouched. If you pass the wrong updateMask string, the update will not take effect. If a field mask is not present on update, the operation applies to all fields (as if a field mask of all fields has been specified). So if you want to update all fields, just set the updateMask empty.\n4. Any field that has a zero value is not shown in the response."]]