mercoledì 2 marzo 2016

JEE & Microservices: how to create a JAX-RS 2.0 service with Eclipse & WebLogic 12.2.1

WebLogic 12.2.1 is a JEE 7 Applicatin Server.

Java EE 7 with JAX-RS 2.0 brings several useful features, which further simplify development and lead to the creation of even more-sophisticated, but lean, Java SE/EE RESTful applications.

A JAX-RS 2.0 component deployed on the Multitenancy (Domain Partitions) of WLS12.2.1, can be considered/is a java microservices:

  • It has X,Y,Z Scalability
    • For Z Scalability (data partitioning) Oracle propose also advanced features with Pluggable databases (Oracle DB Multitenancy) or with Multitenancy of Coherence.
    • X & Y Scaling are guaranteed with Dynamic Cluster and Domain Partitions
  • Isolation and resource consumptions (i.e CPU, RAM, ...) are guaranteed by Domain Partitions
  • You can Export or Import Domain Partition from On-Premise to Cloud or from Test to Production Environment.
I will explain now how to write a JAX-RS componet, I will create a simple HelloWorld, but in the same way you can create JEE components like an Enterprise Java Bean.

In the following example I have used the following software:
First of all start your Eclipse (OEPE) and choose a name for your workspace:

Add a Server (WebLogic 12.2.1) to your workspace:
Click on Window->Show View->Other ...

In this pop-up choose Server->Servers and click ok:

In the new Server Tab, click on the "... create a new server " link :

Select Oracle and then on Weblogic 12.2.1:
Click on Next:

Select your local installation of Weblogic 12.2.1 and your java home:
Click on Next:

Select your local wls 12.2.1 domain:
Click on Finish:

Now you can see your Weblogic 12.2.1 in your Server Tab:

We can create now the JAX-RS project.
Click on File->New->Other ...

In the following Wizard select Web->Dynamic Web Project:
Click on Next:

Choose a Project Name:
Verify that WebLogic 12.2.1 is selected as Target runtime:
Click on Next:

Click on Next:

Select the Generate web.xml deployment descriptor:

In the Open Associated Perspective pop-up, select Remember my decision:
Click on Yes:

Now we must set some properties of the project, select your project in the Project Explorer (jax-rs-prj) then select Project->Properties

Select JAX-RS (Rest Web Services) 2.0, then verify that in Runtimes tab WebLogic 12.2.1 is selected.
Click on Apply
Click on OK

Now we can write our code, to do this we start with the creation of a java package:
Click on  File->New->Other ...

In this wizard select Java->Package
Click on Next:

Leave the default path for the package <your prj>/src
Choose a name for the package (com.marini.jaxrs)
Click on Finish:

We can create now, under our package, some java classes:
Right click with the mouse on the new package (com.marini.jaxrs) and select New->Class:

The only thing to do here is to choose a name for your java class (ExampleApplication):
Click on Finish:

This is your newly created java Class:

We can create now a new java Class: (repeat the previous operation)

Choose HelloWorldService as a name for your new java Class:

Now, to be able to crete a Rest Service, we need to add few lines of code in our java classes:

package com.marini.jaxrs;


public class ExampleApplication extends Application {


Some Info:
  • @ApplicationPath  annotation defines the context of web services.
  • @ApplicationPath("/rest")  means that all web services will have a context root as “/rest”

package com.marini.jaxrs;


public class HelloWorldService {

public String sayHello() {
return "<h1>Hello World</h1>";


Some Info:
  • @Path("HelloWorld")  applied at class level means that the URI to invoke the methods of this class should have “/HelloWorld” as suffix.
  • @GET annotation on sayHello() method denotes that when a HTTP GET method is invoked, thensayHello()  method should be called.
  • @Path("/sayHello")  means that when HTTP GET method is called with “/sayHello” thensayHello()  method should be executed.

We can start WebLogic Server and deploy on it the jax-rs-prj application, to do this, click with the right button of the mouse on the project (jax-rs-prj) and select Run As->1 Run on Server:

Click on Next:

Verify that the Application jax-rs-prj is in the "Configured" section:
Click on Next:

Wait untill you can read in the Console Tab that the server is in RUNNING mode:

Now that the server is up and running, and the application is deployed, you can test your rest service with the following url:

1 commento: