Java 用予約サーバー(REST)スケルトン
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
リポジトリのクローンを作成して、スケルトンの REST サーバーをダウンロードできます。
git clone https://maps-booking.googlesource.com/java-maps-booking-rest-server-v3-skeleton
はじめに
これは、以下に基づく API v3 予約サーバーの参照実装です。
- google-protobuf
- Jersey RESTful ウェブサービス
前提条件
インストールが必要なソフトウェア
使ってみる
- Proto インターフェースを proto ファイル(api_v3.proto)にコピーします。プロジェクトに合わせてパッケージを変更します(com.partner.mapsbooking.v3.model)。
- IDE で booking_server_v3 という名前のウェブ アプリケーション プロジェクトを作成し、このプロジェクトに Maven サポートを追加します。
- proto ファイルを src/main/resources に配置し、Jersey とプロトコル バッファ ランタイムの依存関係を Maven の pom.xml ファイルに追加します。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.11.0</version>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
<jersey.version>2.23.2</jersey.version>
</properties>
-
src/main で次のコマンドを実行して、proto ファイルで定義されたクラスのソースファイルを自動生成します。
protoc --java_out=java resources/api_v3.proto
- 順番待ちリスト機能を実装する場合は、次も実行します。
protoc --java_out=java resources/waitlist.proto
-
src/main/java 内に、groupId(com.partner.mapsbooking)に一致する新しいパッケージを作成します。リポジトリからサンプルコードを取得します。
git clone https://maps-booking.googlesource.com/java-maps-booking-rest-server-v3-skeleton
ファイルをパッケージの下に配置し、TODO に沿って実装を完了します。
- web.xml ファイルを変更してサーブレットを構成します。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Booking Rest Server</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.partner.mapsbooking</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Booking Rest Server</servlet-name>
<url-pattern>/mapsbooking/*</url-pattern>
</servlet-mapping>
</web-app>
- [実行構成] で、Tomcat サーバー構成を設定します。すべての jar を /WEB_INF/lib ディレクトリに追加します(プロジェクト構造 -> アーティファクト -> すべての jar を選択して右クリックし、[/WEB-INF/lib に配置] を選択します)。
- Tomcat を実行してサーバーを起動します。
最終的なディレクトリ構造
src
|---main
|---java
|---com.partner.mapsbooking
|---rest
|---BookingService.java
|---BookingExceptionMapper.java
|---Error.java
|---authentication
|---AuthenticationService.java
|---RestAuthenticationFilter.java
|---v3.model
|---ApiV3.java
|---Waitlist.java
|---resources
|---api_v3.proto
|---waitlist.proto
|---test
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-26 UTC。
[null,null,["最終更新日 2025-07-26 UTC。"],[[["\u003cp\u003eThis guide helps you set up a Java-based REST server for the Booking API v3 using protocol buffers and Jersey.\u003c/p\u003e\n"],["\u003cp\u003eYou need to install Apache Maven, Protocol Compiler for Java, and Apache Tomcat before starting.\u003c/p\u003e\n"],["\u003cp\u003eThe setup involves generating code from proto files, configuring a web application, and deploying it on Tomcat.\u003c/p\u003e\n"],["\u003cp\u003eYou can find a skeleton REST server and sample code in the provided GitHub repository to aid in the implementation.\u003c/p\u003e\n"],["\u003cp\u003eThe final directory structure shows how the project should be organized, including source code, resources, and test files.\u003c/p\u003e\n"]]],["First, clone the skeleton REST server repository. Then, copy the Proto Interface into a proto file and create a web application project with Maven support. Place the proto file under `src/main/resources` and add Jersey and protocol buffer dependencies to `pom.xml`. Execute `protoc` commands to generate source files. Retrieve sample code from the skeleton repo, and place the files under `src/main/java`. Configure the servlet in `web.xml`, set up a Tomcat server configuration, and run Tomcat to start the server.\n"],null,["# Booking Server (REST) Skeleton for Java\n\nYou can download our\n[skeleton REST server](https://maps-booking.googlesource.com/java-maps-booking-rest-server-v3-skeleton/) by cloning the repo \n\n```java\ngit clone https://maps-booking.googlesource.com/java-maps-booking-rest-server-v3-skeleton\n```\n\nIntroduction\n------------\n\nThis is a reference implementation for API v3 Booking server based on:\n\n- [](/protocol-buffers/docs/overview)google-protobuf\n- [](https://jersey.github.io/)Jersey RESTful Web Services\n\n### Prerequisites\n\nRequire installations of\n\n- [Apache Maven](https://maven.apache.org/)\n- [Protocol compiler for java](https://github.com/google/protobuf)\n- [Apache Tomcat](http://tomcat.apache.org/)\n\n### Get Started\n\n1. Copy the [Proto Interface](/actions-center/verticals/reservations/waitlists/reference/booking-server-api-rest/e2e-definitions/proto-interface) into a proto file (api_v3.proto). Modify the package to match your project (com.partner.mapsbooking.v3.model).\n - If implementing waitlist functionality, repeat the same steps with the [Waitlist Proto Interface](/actions-center/verticals/reservations/waitlists/reference/booking-server-api-rest/e2e-definitions/proto-interface)\n2. Create a web application project in your IDE named booking_server_v3, add Maven support to this project.\n3. Place your proto file under the **src/main/resources,** add dependencies for Jersey and protocol buffers runtime to the Maven **pom.xml** file: \n\n ```java\n \u003cdependencyManagement\u003e\n \u003cdependencies\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003eorg.glassfish.jersey\u003c/groupId\u003e\n \u003cartifactId\u003ejersey-bom\u003c/artifactId\u003e\n \u003cversion\u003e${jersey.version}\u003c/version\u003e\n \u003ctype\u003epom\u003c/type\u003e\n \u003cscope\u003eimport\u003c/scope\u003e\n \u003c/dependency\u003e\n \u003c/dependencies\u003e\n \u003c/dependencyManagement\u003e\n\n \u003cdependencies\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003eorg.glassfish.jersey.containers\u003c/groupId\u003e\n \u003cartifactId\u003ejersey-container-servlet-core\u003c/artifactId\u003e\n \u003c/dependency\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003eorg.glassfish.jersey.media\u003c/groupId\u003e\n \u003cartifactId\u003ejersey-media-json-jackson\u003c/artifactId\u003e\n \u003cversion\u003e2.27\u003c/version\u003e\n \u003c/dependency\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.protobuf\u003c/groupId\u003e\n \u003cartifactId\u003eprotobuf-java\u003c/artifactId\u003e\n \u003cversion\u003e3.5.1\u003c/version\u003e\n \u003c/dependency\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003eio.grpc\u003c/groupId\u003e\n \u003cartifactId\u003egrpc-protobuf\u003c/artifactId\u003e\n \u003cversion\u003e1.11.0\u003c/version\u003e\n \u003c/dependency\u003e\n \u003c/dependencies\u003e\n\n \u003cproperties\u003e\n \u003cjava.version\u003e1.8\u003c/java.version\u003e\n \u003cjersey.version\u003e2.23.2\u003c/jersey.version\u003e\n \u003c/properties\u003e\n ```\n4. Execute the following command under **src/main** to auto-generate a\n source file for the classes defined in the proto file:\n\n ```java\n protoc --java_out=java resources/api_v3.proto\n ```\n - If implementing waitlist functionality, also execute the following: protoc --java_out=java resources/waitlist.proto\n5. Inside of the **src/main/java**, create a new package matching your\n groupId (com.partner.mapsbooking). Retrieve the sample code from the\n repo:\n\n ```java\n git clone https://maps-booking.googlesource.com/java-maps-booking-rest-server-v3-skeleton\n ```\n\n place the files under your package, follow the **TODOs** to complete\n your implementation.\n6. Configure your servlet by modifying the **web.xml** file: \n\n ```java\n \u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n \u003cweb-app xmlns=\"http://xmlns.jcp.org/xml/ns/javaee\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xsi:schemaLocation=\"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd\"\n version=\"4.0\"\u003e\n\n \u003cservlet\u003e\n \u003cservlet-name\u003eBooking Rest Server\u003c/servlet-name\u003e\n \u003cservlet-class\u003eorg.glassfish.jersey.servlet.ServletContainer\u003c/servlet-class\u003e\n \u003cinit-param\u003e\n \u003cparam-name\u003ejersey.config.server.provider.packages\u003c/param-name\u003e\n \u003cparam-value\u003ecom.partner.mapsbooking\u003c/param-value\u003e\n \u003c/init-param\u003e\n \u003cload-on-startup\u003e1\u003c/load-on-startup\u003e\n \u003c/servlet\u003e\n\n \u003cservlet-mapping\u003e\n \u003cservlet-name\u003eBooking Rest Server\u003c/servlet-name\u003e\n \u003curl-pattern\u003e/mapsbooking/*\u003c/url-pattern\u003e\n \u003c/servlet-mapping\u003e\n \u003c/web-app\u003e\n ```\n7. In the Run Configurations, set up a Tomcat server configuration. Add all the jars to the /WEB_INF/lib directory (project structure -\\\u003e artifacts -\\\u003e After selecting all jars right click and choose \"Put into /WEB-INF/lib\").\n8. Run Tomcat to start your server.\n\n### Final Directory Structure\n\n```java\n src\n |---main\n |---java\n |---com.partner.mapsbooking\n |---rest\n |---BookingService.java\n |---BookingExceptionMapper.java\n |---Error.java\n |---authentication\n |---AuthenticationService.java\n |---RestAuthenticationFilter.java\n |---v3.model\n |---ApiV3.java\n |---Waitlist.java\n |---resources\n |---api_v3.proto\n |---waitlist.proto\n |---test\n```"]]