In preparation to one of my DOAG 2014 talks „Java WebApps and Services in Oracle Cloud" I created a new trial account for Oracle Java Cloud Service (http://cloud.oracle.com) two weeks ago. Now was the time to create a RESTful Service and deploy it to the cloud and make it accessible to public.
Last time I tried the Oracle Cloud I used exclusively JDeveloper, this time I made my experience with Netbeans. From my point of view Netbeans has pretty good support for generating RESTService from Database tables. In a couple of minutes it is possible to create CRUD Operations and make it accesible by a REST endpoint. No matter what content type you prefer, by using JAXB XML and JSON content types are both automatically available.
Step 1: Create an Oracle Java Cloud Service trial account (fee less for 30 days)
Step 2: Download and install Netbeans Version 8.01 (http://www.netbeans.org)
Step 3: Make sure to install the Oracle Cloud plugin (1.5) in Netbeans
Step 4: Download and Install (just unpack) Oracle Java Cloud Service - Saas Extension SDK (release 14.1.12.0 Find it here: http://www.oracle.com/technetwork/middleware/weblogic/downloads/java-cloud-sdk-1848874.html)
Step 5: Next add your Oracle Cloud account as Cloud Provider to Netbeans (Goto „Services" Tab and provide your cloud account details)
Step 6: Add a WebLogic Service Instance under ‚Services Tab', so you are able to test the RESTService locally prior to deploying to Oracle Cloud. (I am not going into detail in this post for that task)
Step 7: Make sure to deploy your DB Schema objects plus data into Oracle Database Cloud Service (when subscribing to Java Cloud Service you will get the DB Service as well). See in my previous posts for more details.
HowTo develop RESTService with Netbeans
Everything is setup for cloud deployment. Now let's create a simple REST-Service on top of the HR DB Tables EMPLOYEES and DEPARTMENTS.
Step 1: Start Netbeans, Create new Project Wizard from Type „Maven / Web Application"
Step 2: Enter your desired groupId, ArtefactId etc.
Step 3: Choose Oracle WebLogic as ‚Server' and Java EE Version 5.
Click Finish.
Step 3: Start „RESTFul Services from Database" Wizard
Step 4: Make a connection to your local database and select the desired Tables.
Step 5: Review and adapt Package-Settings if desired
Next:
Confirm your input and let Netbeans generate the source code. If you like you can make some adjustments to the generated code. Typically I would change the REST resource path. (in my sample: to „employees")
Step 6: In order beeing deployable to Oracle Cloud we need to make further some adjustments. That
is
Adjustment 1: web.xml
(1) We need to change the servlet class from org.glassfish.jersey.servlet.ServletContainer to com.sun.jersey.spi.container.servlet.ServletContainer because the prior class is not available on the cloud server. (2) Further add an <login-config /> if you want to make the Service publicly (without authentication) available.
Adjustment 2: weblogic.xml
Add a library reference to jax-rs 1.1
Adjustment 3: persistence.xml
Configure your Cloud DB Service name as JNDI Data Source
Deploy RESTService to the cloud
Step 1: Open Project Properties, Choose ‚Oracle Cloud Remote" Server Type, Java EE Version 5
Step 2: Execute ‚Run'
The cloud deployment will start...
The WAR file will be virus scanned...
And Finally deployed to the cloud.
Test the cloud RESTService
Here is my REST Service url for departments https://jcssaasextensitrial9655-deenpitconsultrial83769.java.us2.oraclecloudapps.com/HRRestService/webresources/departments
If using Chrome install the Postman extension to test the Service. e.g. to test different content types. JSON, XML
Administer Oracle Java Cloud Service from within Netbeans
Right under the Service Tab your able to Open the Apps, Start, Stop or Undeploy.
To view the Job Status and Logs open the corresponding view from the Oracle Cloud entry.
Troubleshooting
If something goes wrong open the Cloud Job View and Logs Tab to check the details, e.g. if a deployment fails. (1) ClassNotFound Exception
Solution in that particular case is: Use com.sun.jersey.spi.container.servlet.ServletContainer instead of org.glassfish.jersey.servlet.ServletContainer.
(2) java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder
Solution: Somehow the generated POM had a fixed reference to JAX-RS Library which is provided from the cloud server. So I changed that dependency
Conclusion
- Having everything setup (Cloud SDK, local WebLogic Servece, DB Objects in the Cloud Service) it is quite easy to build and deploy RESTful services.
- Why is the SDK not bundled with the Netbeans Oracle Cloud Plugin?
- I wish there would be that kind of wizard (RESTful Services from Database) for JDeveloper ;)
- I wish there would be that kind of wizard (RESTful Services from Database) for JDeveloper ;)
Download Sample: enpit.training.cloud.hrservice-netbeans801.zip
Further Information
- http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/JCS-JAXRS/jcs_jaxrs.html (Tutorial)
- See also: http://www.oracle.com/technetwork/articles/java/enterprise-cloud-2227135.html (Build with NetBeans IDE, Deploy to Oracle Java Cloud Service)