commit | fd87ac6937070b1a7cce1255f8a06483daed24a3 | [log] [tgz] |
---|---|---|
author | Yunsi Mou <yunsi@yunsi.hot.corp.google.com> | Thu Apr 26 17:50:19 2018 -0400 |
committer | Yunsi Mou <yunsi@yunsi.hot.corp.google.com> | Thu Apr 26 17:50:19 2018 -0400 |
tree | 14f5dab21933e569b334a05397c79da76e18b5e5 |
Initial commit of sample java rest server
This ia a reference implementation for API v3 Booking server based on:
Require installations of
Copy the Proto Interface into a proto file (api_v3.proto). Modify the package to match your project (com.partner.mapsbooking.v3.model).
Create a web application project in your IDE named booking_server_v3, add Maven support to this project.
Place your proto file under the src/main/resources, add dependencies for Jersey and protocol buffers runtime to the Maven pom.xml file:
<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>
Execute the following command under src/main to auto-generate a source file for the classes defined in the proto file:
protoc --java_out=java resources/api_v3.proto
Inside of the src/main/java, create a new package matching your groupId (com.partner.mapsbooking). Retrieve the sample code from the repo, place the files under your package, follow the TODOs to complete your implementation.
Configure your servlet by modifying the web.xml file:
<?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>
In the Run Configurations, set up a Tomcat server configuration. Add all the jars to the /WEB_INF/lib directory (project structure -> artifacts -> After selecting all jars right click and choose “Put into /WEB-INF/lib”).
Run Tomcat to start your server.
src |---main |---java |---com.partner.mapsbooking |---rest |---BookingService.java |---BookingExceptionMapper.java |---Error.java |---authentication |---AuthenticationService.java |---RestAuthenticationFilter.java |---v3.model |---ApiV3.java |---resources |---api_v3.proto |---test