Thursday, July 30, 2015

Step by Step tutorial on how to create a weblogic RESTFull web service with eclipse and OEPE



Step by Step tutorial on how to create a weblogic RESTFull web service with eclipse and OEPE
Contents
1.  Download and install eclipse
2.  Install OEPE on your eclipse
3. Create a web service project “TestWSProj”
4. Create the web service class and methods
5. Setup web.xml and weblogic.xml for REST Web services
6. Insure weblogic has the jax-rs libraries deployed and targeted for the domain you will be using
7. Create a weblogic server in the eclipse


I will show how to create a restfull web service for WebLogic using eclipse and OEPE

1.     Download and install eclipse

I used 64bit windows Luna. anything should work.
Google it.

2.     Install OEPE on your eclipse

Eclipse->help->install new software
Work with:
OEPE repo. URL  can be find by googling ‘OEPE’
Then select the 2 weblogic items in tools folder:

3. Create a web service project “TestWSProj”

File->new->other, Web->Dynamic Web Project
Name: TestWSProj
Target runtime: Oracle WebLogic 11gR1 (10.3.6)
(change it if you have a different version)
Dynamic web module: 2.5
(this is the servlet specification level, oracle weblogic 10.3 (11g) only supports up to 2.5, oracle 12c supports 3.0)
Configuration:
Default configuration for oracle weblogic server 11gR1 (10.3.6)
 Next-> Next
Mark the ‘generate web.xml deployment descriptor’ with a ‘v’
Finish
Add a REST webservice api library to the project
We will use JAX-RS.
Download the JAX-RS jar and add it to the webcontent/web-inf/lib directory (the standard location for jars in web projects). Google jax-rs for download.
I am using jax.rs 1.1 for this example (taken from http://repo1.maven.org/maven2/javax/ws/rs/jsr311-api/1.1.1/jsr311-api-1.1.1.jar)

4. Create the web service class and methods


Right click on the src in your project and create a package (new->package) give it a name ‘com.test’)
Right click on the new package and create a new REST web service (new->other, webservices->REST web service)

Click ‘Next’
Give it a class name ‘TestWS’

Next
Now you choose the MIME type used for consumers and producers of the webservice (the protocols used to pass data to web service methods and the protocols used to return data from them).
I will select the simplest text/plain for this example :


Next
Now you will see the name of the package and class for the application config class – leave it default and click
Finish
The following classes were created:
Now we will create a method in TestWS.java, add the following method:
@GET
      @Produces("test/plain")
      @Path("webservicemethod1")
      public String webservicemethod1()
      {
            return "It works!";
      }

 


5. Setup web.xml and weblogic.xml for REST Web services

You will find both these files in webcontent/web-inf folder.
Web.xml is the standard java servlet container config file.
Weblogic.xml is a web logic (oracle’s implementation of the servlet container) specific  config file.

Web.xml changes

Add the following to web.xml somewhere under web-app tag:
(note: when double clicking on web.xml or weblogic.xml you might get the ‘design view’, click on ‘source’ tab on the bottom of the windows to change it)
<display-name>TestWSProj</display-name>
<servlet>
       <servlet-name>JAX-RS Application</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>rest.application.config.ApplicationConfig</param-value>
       </init-param>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping> 
So now we have:


Save it.
This tells web logic what classes are used as servlet containers and application config.

Weblogic.xml changes

Add the following (under wls:weblogic-web-app tag) in weblogic.xml:
<wls:weblogic-version>10.3.6</wls:weblogic-version>
<wls:context-root>TestWSProj</wls:context-root>
<wls:library-ref>
        <wls:library-name>jax-rs</wls:library-name>
        <wls:specification-version>1.1</wls:specification-version>
        <wls:implementation-version>1.9</wls:implementation-version>
        <wls:exact-match>false</wls:exact-match>
    </wls:library-ref>
This tells web logic server that it should use the shared deployed libraries of jax-rs (see later the section about insuring deployment of these jars on web logic server)
So now it should look like this:

6. Insure weblogic has the jax-rs libraries deployed and targeted for the domain you will be using


Go to your weblogic admin console and click the deployments :

If these are not deployed, you will need to download them ( http://repo1.maven.org/maven2/javax/ws/rs/jsr311-api/1.1.1/jsr311-api-1.1.1.jar ) and deploy them yourself.

7. Create a weblogic server in the eclipse

Make sure you are in the JavaEE perspective (top right in eclipse)
Click the servers tab on the bottom. Right click the background of the windows and select new->server
Select the server you want to work with:


Click Next.
In the following screen, you can choose an existing domain on your web logic server or create a new one (if you choose to create a new one, click the three yellow star icon and you will get a create domain dialog , call it ‘testDomain’ and leave everything default,  when you click finish, it will run a WLST script and create the domain)


Click Next
Now choose the TestWSProj on the left side and click ‘Add’
Click Next-Next & Finish
The console tab should show if there were any errors and A new server will be added to the list in the servers tab
This server can be started and code can be published and republished to it (right click on the server)

8. Testing the web service
Make sure the weblogic server is running and there is no “republish” message near it (if there is then right click the server and choose publish)
Open abrowser with the following URL:
you should get the following message:
“It works!”