Before You Begin
This 15-minute tutorial shows you how to deploy a web service to Oracle Application Container Cloud Service using the Dropwizard framework.
Background
Dropwizard is an open source Java framework that you can use to quickly create RESTful web services. Dropwizard integrates several components and Java frameworks that can help you build more robust applications.
In this tutorial, you create a web service for an employee directory application and then deploy it to Oracle Application Container Cloud Service. Data for the application is stored in an in-memory database. You test the REST service by using an HTML5 JavaScript client.
What Do You Need?
- Access to an instance of Oracle Application Container Cloud Service
- NetBeans or another code editor
- Oracle's Java Development Kit 8 (JDK 8)
- Apache Maven 3.0+
- The HTML5 JavaScript client files: employees-client.zip
- The Dropwizard Maven project: employee-service-dropwizard.zip
Create the Java Project with Maven
- Open a terminal window (Linux or Mac) or the command prompt (Windows), in the folder you want to create the project in, type the following command:
mvn archetype:generate -DgroupId=com.dropwizard.employee.service -DartifactId=employee-service-dropwizard -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
- Using a text editor, open the
pom.xml
file. - To add the project dependencies copy this code snippet and paste it into the
<dependencies>
tag. - Add the following Maven compile properties under the root element.
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
- To add the Maven Shade Plugin by copy this code snippet under the root element of the
pom.xml
file.
Create the Web Service
- In the source directory of your maven project, create the
EmployeeApplication.java
file in thecom.dropwizard.employee.service
package and copy and paste this code snippet into the file.
The main entry point into a Dropwizard application is theApplication
class. It contains themain
method that is executed when you run the JAR file. Theinitialize
method is used to configure aspects of the application required before the application runs, like bundles, configuration source providers, and so on.
Therun
method pulls together all the resources, servlets, filters, health checks, Jersey providers, managed objects, tasks, and Jersey properties which your application provides. Notice the definition ofeResource
loads the REST definitions into the application. TheMultiPartFeature
class is a helper for the application. TheCrossOriginEmployeeFilter
class defines the cross origin HTTP headers for the application. - Create the
EmployeeResource.java
file in your Maven project and copy this code snippet into your file.
This class defines how REST requests are handled by the Dropwizard application. All the standard REST operations are defined. A Data Access Object (DAO) is used to update the in memory datasource. The DAO can easily be swapped for a database DAO without changing the application code. - Create the
employee.yaml
file in the root of your Maven project. This is the same directory that contains yourPOM.xml
file. Add the following content to theemployee.yaml
file:server: applicationConnectors: - type: http port: ${PORT} # Logging settings. logging: level: INFO
Theemployee.yaml
configuration file gets thePORT
environment variable and sets the network port for the application. When the application is deployed to Oracle Application Container Cloud Service, the application uses the network port that is assigned by the service. - Add the following support classes to your project from the
source
directory of the solution file. Copy the files to thesrc/main/java/com/dropwizard/employee/service
directory of your maven project.Class File Name Description Employee.java Defines the structure of the data used in the employee directory. CrossOriginEmployeeFilter.java Defines the cross origin HTTP headers for your application. EmployeeConfiguration.java Defines any Dropwizard configuration options, if needed. EmployeeDAO.java Defines database operations for Employee. EmployeeListDAO.java ArrayList
data store forEmployee
objects.MockEmployeeList.java Creates mock data for use with the application.
Prepare the Application for Deployment
- Create the
manifest.json
file in the root directory and add the following content.
{ "runtime": { "majorVersion": "8" }, "command": "java -jar employee-service-dropwizard-1.0-SNAPSHOT.jar server employee.yaml" }
Note: To run a Dropwizard application you must specify the
server
command which requires the YAML configuration file. - Create the
assembly
directory into thesrc
folder. - Create the
distribution.xml
file in theassembly
folder. - Copy this code snippet and paste it into the
distribution.xml
file.
Theformat
element specifies the type of application archive to create.<formats> <format>zip</format> </formats>
Thefileset
element identifies the metadata files.<fileSets> <fileSet> <directory>${project.basedir}</directory> <outputDirectory>/</outputDirectory> <includes> <include>manifest.json</include> <include>employee.yaml</include> </includes> </fileSet>
Thefileset
element also identifies the fat JAR file.<fileSet> <directory>${project.build.directory}</directory> <outputDirectory>/</outputDirectory> <includes> <include>employee-service-dropwizard-1.0-SNAPSHOT.jar</include> </includes> <!-- Some lines excluded to save space. --> </fileSet>
- Using a text editor, open the
pom.xml
file and copy this code snippet and paste it inside the<plugins>
tags at the end of the plugin list. Save the file. - In the command line window, build your application by typing the maven command:
mvn clean package
- In the
target
directory locate theemployee-service-dropwizard-1.0-SNAPSHOT.zip
file which will be used to deploy your application to Oracle Application Contianer Cloud Service.
The distribution.xml
script creates a zip file that includes the fat JAR (employee-service-dropwizard-1.0-SNAPSHOT.jar)
, the manifest.json
file, and the employee.yaml
file.
Deploy the Application
- Open the Oracle Application Container Cloud Service console.
- In the Applications list view, click Create Application and select Java SE.
- In the Application section, enter a name for your application, and then click Choose File next to Application.
-
In the File Upload dialog box,
select the
employee-service-dropwizard-1.0-SNAPSHOT.zip
file, and click Open. - Click Create.
Note: Your application could take a few minutes to deploy.
Test the Application
- Extract the
employee_client.zip
file in your local system. - Using a text editor, open
EmployeeController.js
file. - Edit the
urlService
variable and put in the generated link of your application deployed in Oracle Application Container Cloud Service.
$scope.urlService = "http://javaexample-mydomain.apaas.us.oraclecloud.com/employees";
- In a web browser open the
index.html
file and then click Add New.
Description of this image - Enter the First Name, Last Name, Email, Phone, Birthdate, Title, and Department values.
Description of this image - Beside photo click Browse... and then select an image file located in the
photos
folder.
Description of this image - Click Save.
Description of this image - Test the delete, update, and search options.
Want to Learn More?
- Oracle Application Container Cloud Service in the Oracle Help Center
- Dropwizard website dropwizard.io