Oracle by Example brandingDeploy a Dropwizard Application to Oracle Cloud

section 0Before 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?


section 1Create the Java Project with Maven

  1. 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
  2. Using a text editor, open the pom.xml file.
  3. To add the project dependencies copy this code snippet and paste it into the <dependencies> tag.
  4. 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> 
  5. To add the Maven Shade Plugin by copy this code snippet under the root element of the pom.xml file.

section 2Create the Web Service

  1. In the source directory of your maven project, create the EmployeeApplication.java file in the com.dropwizard.employee.service package and copy and paste this code snippet into the file.

    The main entry point into a Dropwizard application is the Application class. It contains the main method that is executed when you run the JAR file. The initialize method is used to configure aspects of the application required before the application runs, like bundles, configuration source providers, and so on.

    The run 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 of eResource loads the REST definitions into the application. The MultiPartFeature class is a helper for the application. The CrossOriginEmployeeFilter class defines the cross origin HTTP headers for the application.
  2. 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.
  3. Create the employee.yaml file in the root of your Maven project. This is the same directory that contains your POM.xml file. Add the following content to the employee.yaml file:
    server:    
        applicationConnectors:
        -   type: http
            port:  ${PORT}
        
    # Logging settings.
    logging:
        level: INFO
    

    The employee.yaml configuration file gets the PORT 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.
  4. Add the following support classes to your project from the source directory of the solution file. Copy the files to the src/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 for Employee objects.
    MockEmployeeList.java Creates mock data for use with the application.

section 3Prepare the Application for Deployment

  1. 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.

  2. Create the assembly directory into the src folder.
  3. Create the distribution.xml file in the assembly folder.
  4. Copy this code snippet and paste it into the distribution.xml file.

    The format element specifies the type of application archive to create.
      <formats>
        <format>zip</format>
      </formats>
    

    The fileset element identifies the metadata files.
      <fileSets>
        <fileSet>
          <directory>${project.basedir}</directory>
          <outputDirectory>/</outputDirectory>
          <includes>
            <include>manifest.json</include>
            <include>employee.yaml</include>
          </includes>
        </fileSet>
    

    The fileset 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>
    
  5. 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.

  6. 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.

  7. In the command line window, build your application by typing the maven command:
    mvn clean package
  8. In the target directory locate the employee-service-dropwizard-1.0-SNAPSHOT.zip file which will be used to deploy your application to Oracle Application Contianer Cloud Service.

section 4Deploy the Application

  1. Open the Oracle Application Container Cloud Service console.
  2. In the Applications list view, click Create Application and select Java SE.
  3. In the Application section, enter a name for your application, and then click Choose File next to Application.
  4. In the File Upload dialog box, select the employee-service-dropwizard-1.0-SNAPSHOT.zip file, and click Open.
  5. Click Create.

    Note: Your application could take a few minutes to deploy.

section 5Test the Application

  1. Extract the employee_client.zip file in your local system.
  2. Using a text editor, open EmployeeController.js file.
  3. 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";
  4. In a web browser open the index.html file and then click Add New.
    Employee client home page
    Description of this image
  5. Enter the First Name, Last Name, Email, Phone, Birthdate, Title, and Department values.
    Add New employee page
    Description of this image
  6. Beside photo click Browse... and then select an image file located in the photos folder.
    File upload window
    Description of this image
  7. Click Save.
    Employee client home page
    Description of this image
  8. Test the delete, update, and search options.

more informationWant to Learn More?