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:
- Have access to a paid or trial Oracle Java Cloud instance. You can request a free trial if needed.
- Install a local copy of WebLogic Server 10.3.6. A free developer zip distribution can downloaded from the WebLogic Server 12c and WebLogic Server 11g Releases page.
- Install the Oracle Java Cloud SDK
- Configure NetBeans to use your Oracle Java Cloud server. For details instructions please see the Configuring NetBeans for Oracle Cloud OBE.
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.
-
Using the Services tab in NetBeans, start the local WebLogic Server 10.3.6 instance.
-
Open a web browser and log in to the the WebLogic Admin Console at http://localhost:7001/console/
-
In the WebLogic Admin Console open the Deployments page.
-
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.
Press the Next button.
Select the option to Install this deployment as a library and press the Next button.
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
-
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.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.
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.
-
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> -
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. -
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.
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.
-
Begin by creating a new Java class in the CloudRESTdemo project.
-
Name the class RootResource and place it in a com.example package. Press Finish.
-
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";
}
} -
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.
-
Create another new Java class in the CloudRESTdemo project.
-
Name the class MyApplication and place it in a com.example package. Press Finish.
-
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 yourweb.xml
file.
Locally testing the JAX-RS resource
-
Right-click on the
CloudRESTdemo
project and select Deploy. The project should deploy without errors. -
In a web browser go to http://localhost:7001/CloudRESTdemo/resources/message. You should receive a text message response.
Deploying to the Oracle Java Cloud
-
Right-click on the
CloudRESTdemo
project and select Properties. In theRun category change the Server to Oracle Cloud Remote. Make sure that Display Browser on Run is selected and press OK. -
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.
You should receive a text message response.
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
- Oracle Cloud Learning Center : Developing RESTful Web Services
- Jersey 1.9 User Guide
- Oracle Fusion Middleware Programming Advanced Features of JAX-WS Web Services for Oracle WebLogic Server 11g Release 1 (10.3.6)
- CloudRESTdemo NetBeans solution project
Credits
- Lead Curriculum Developer: Matt Heimer