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.xmlfile. - 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.xmlfile.
Create the Web Service
- In the source directory of your maven project, create the
EmployeeApplication.javafile in thecom.dropwizard.employee.servicepackage and copy and paste this code snippet into the file.
The main entry point into a Dropwizard application is theApplicationclass. It contains themainmethod that is executed when you run the JAR file. Theinitializemethod is used to configure aspects of the application required before the application runs, like bundles, configuration source providers, and so on.
Therunmethod pulls together all the resources, servlets, filters, health checks, Jersey providers, managed objects, tasks, and Jersey properties which your application provides. Notice the definition ofeResourceloads the REST definitions into the application. TheMultiPartFeatureclass is a helper for the application. TheCrossOriginEmployeeFilterclass defines the cross origin HTTP headers for the application. - Create the
EmployeeResource.javafile 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.yamlfile in the root of your Maven project. This is the same directory that contains yourPOM.xmlfile. Add the following content to theemployee.yamlfile:server: applicationConnectors: - type: http port: ${PORT} # Logging settings. logging: level: INFO
Theemployee.yamlconfiguration file gets thePORTenvironment 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
sourcedirectory of the solution file. Copy the files to thesrc/main/java/com/dropwizard/employee/servicedirectory 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 ArrayListdata store forEmployeeobjects.MockEmployeeList.java Creates mock data for use with the application.
Prepare the Application for Deployment
- Create the
manifest.jsonfile 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
servercommand which requires the YAML configuration file. - Create the
assemblydirectory into thesrcfolder. - Create the
distribution.xmlfile in theassemblyfolder. - Copy this code snippet and paste it into the
distribution.xmlfile.
Theformatelement specifies the type of application archive to create.<formats> <format>zip</format> </formats>
Thefilesetelement identifies the metadata files.<fileSets> <fileSet> <directory>${project.basedir}</directory> <outputDirectory>/</outputDirectory> <includes> <include>manifest.json</include> <include>employee.yaml</include> </includes> </fileSet>
Thefilesetelement 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.xmlfile 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
targetdirectory locate theemployee-service-dropwizard-1.0-SNAPSHOT.zipfile 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.zipfile, and click Open. - Click Create.
Note: Your application could take a few minutes to deploy.
Test the Application
- Extract the
employee_client.zipfile in your local system. - Using a text editor, open
EmployeeController.jsfile. - Edit the
urlServicevariable 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.htmlfile 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
photosfolder.
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
Deploy a Dropwizard Application to Oracle Cloud