Creating JAX-RS 1.1 Web Services on the Oracle Java Cloud

 

Overview

Purpose

This tutorial covers how to create a basic JAX-RS web service and deploy it to the Oracle Java Cloud.

Time to Complete

Approximately 1 hour.

Introduction

Deploying a JAX-RS 1.1 RESTful web service to the Oracle Java Cloud requires specific project configuration. This OBE will explain the steps required to locally develop a JAX-RS web service and then deploy it to an Oracle Java Cloud instance.

Hardware and Software Requirements

The following is a list of hardware and software requirements:

  • NetBeans 7.3 or later
  • WebLogic Server 10.3.6
  • An Oracle Java Cloud instance (paid or trial)

Prerequisites

Before starting this tutorial, you should:

 

Enabling JAX-RS 1.1 in WebLogic Server 10.3.6

The Oracle Java Cloud service is based on WebLogic Server 10.3.6. To make development easier you should target a local WebLogic Server 10.3.6 instance until your application is ready to be deployed to the Oracle Cloud. Because WebLogic Server 10.3.6 is a Java EE 5 application server it would not normally support JAX-RS which is a component of Java EE 6. However the Oracle Java Cloud already is configured to provide JAX-RS 1.1 support through the inclusion of Jersey 1.9. Jersey is the reference implementation of the JAX-RS specification.

Jersey 1.9 is also supplied with a local install of WebLogic Server 10.3.6 but it is not deployed by default. The steps below show you how to deploy the supplied Jersey 1.9 WebLogic shared library to locally support JAX-RS application in a fashion similar to the Oracle Java Cloud.

  1. Using the Services tab in NetBeans, start the local WebLogic Server 10.3.6 instance.

    Start WLS
  2. Open a web browser and log in to the the WebLogic Admin Console at http://localhost:7001/console/

  3. In the WebLogic Admin Console open the Deployments page.

    WLS Deployments Page
  4. Deploy jersey-bundle-1.9.war as a WebLogic shared library.

    In the Deployments page press the Install button.

    Select the jersey-bundle-1.9.war file from within the wlserver\common\deployable-libraries\ directory of your WLS 10.3.6 install.

    Selecting jersey-bundle-1.9.war

    Press the Next button.

    Select the option to Install this deployment as a library and press the Next button.

    Deploying as a library

    Press the Finish on the final page to complete the deployment of jersey-bundle-1.9.war as a library.

 

Creating a Java EE 5 project

  1. Create a Java EE 5 web application project named CloudRESTdemo.

    Within NetBeans select File -> New Project from the menu.

    Select a project type of Web Application and press Next.

    New Java Web Application

    Give the project a name of CloudRESTdemo and press the Next button.

    Select the local Oracle WebLogic Server 10.3.6 instance as the server and press the Finish button.

    Selecting Oracle WebLogic Server 10.3.6 for the project
 

Enabling JAX-RS 1.1 in the Java EE 5 project

The project created in the previous step does not have access to the JAX-RS libraries yet because they are not a standard part of Java EE 5. In these steps you configure your project to use the WebLogic shared Jersey library.

  1. Open the weblogic.xml configuration file and add the <library-ref> section shown below.

    <?xml version="1.0" encoding="UTF-8"?>
    <weblogic-web-app
    xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
    http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd">
    <jsp-descriptor>
    <keepgenerated>true</keepgenerated>
    <debug>true</debug>
    </jsp-descriptor>
    <context-root>/CloudRESTdemo</context-root>
    <library-ref>
    <library-name>jax-rs</library-name>
    <specification-version>1.1</specification-version>
    <implementation-version>1.9</implementation-version>
    <exact-match>true</exact-match>
    </library-ref>
    </weblogic-web-app>
  2. Open the web.xml configuration file and add the <login-config>, <servlet>, and <servlet-mapping> sections shown below.

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <session-config>
    <session-timeout>
    30
    </session-timeout>
    </session-config>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <login-config/>
    <servlet>
    <servlet-name>com.example.MyApplication</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
    <param-name>javax.ws.rs.Application</param-name>
    <param-value>com.example.MyApplication</param-value>
    </init-param>
    </servlet>
    <servlet-mapping>
    <servlet-name>com.example.MyApplication</servlet-name>
    <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>
    </web-app>

    Note that the com.example.MyApplication class does not exist yet, you will create it in a later step.

  3. Either close and reopen the CloudRESTdemo project or restart NetBeans. NetBeans does not dynamically pickup new WebLogic shared libraries specified in weblogic.xml unless your reopen the project or restart NetBeans.

    After reopening the project your should see Jersey related libraries in the project.

    Jersey Shared Libraries
 

Creating JAX-RS resource classes

In this section you create a JAX-RS resource class that returns a simple text message and the required supporting Application class.

  1. Begin by creating a new Java class in the CloudRESTdemo project.

    Creating a new Java class
  2. Name the class RootResource and place it in a com.example package. Press Finish.

    com.example.RootResource
  3. Open the RootResource Java file and add complete it as shown below.

    package com.example;

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path("/message")
    @Produces(MediaType.TEXT_PLAIN)
    public class RootResource {
    @GET
    public String getMessage() {
    return "Hello Cloud World";
    }
    }
  4. If at any point NetBeans presents a popup for REST Resource Configuration then configure the project to use User registration and to not add any Jersey classes to the classpath.

    REST Resource Configuration
  5. Create another new Java class in the CloudRESTdemo project.

    Creating a new Java class
  6. Name the class MyApplication and place it in a com.example package. Press Finish.

    com.example.MyApplication
  7. Open the MyApplication Java file and add complete it as shown below.

    package com.example;

    import java.util.HashSet;
    import java.util.Set;
    import javax.ws.rs.core.Application;

    public class MyApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
    Set<Class<?>> classes = new HashSet<Class<?>>();
    classes.add(RootResource.class);
    return classes;
    }

    }

    This class is configured using a <param-value> element in your web.xml file.

 

Locally testing the JAX-RS resource

  1. Right-click on the CloudRESTdemo project and select Deploy. The project should deploy without errors.

  2. In a web browser go to http://localhost:7001/CloudRESTdemo/resources/message. You should receive a text message response.

    Localhost Browser Test
 

Deploying to the Oracle Java Cloud

  1. Right-click on the CloudRESTdemo project and select Properties. In the Run category change the Server to Oracle Cloud Remote. Make sure that Display Browser on Run is selected and press OK.

    Configuring the Project to run on the Oracle Cloud
  2. Right-click on the CloudRESTdemo project and select Run. The project should deploy without errors and a web browser should automatically open.

    If a authentication dialog box appears then enter your Oracle Cloud credentials.

    Authentication Required dialog

    You should receive a text message response.

    Cloud Test
 

Summary

In this tutorial, you learned to:

  • Configure a project to use JAX-RS 1.1 on WebLogic Server 10.3.6 and on the Oracle Java Cloud.

Resources

Credits

  • Lead Curriculum Developer: Matt Heimer