Week 20 of 2010


This week, my post is going to be technical. So if you are not interested in Web Services, JSON or WebLogic, you can skip this weeks post.  (Crystal and I bought a car over the weekend so I will blog about that next week.)

Lately I have been playing around with facebook’s new graph API. I thought that it would be cool to implement the same type of REST/Web Service/JSON application here at work. I am also getting ready to upgrade some applications from BEA WebLogic 8.1.6 to Oracle WebLogic 10.3. I thought that this would be a good introduction on how to write a new application in the new IDE environment. (Since Oracle purchased BEA, a lot of things that were once familiar are now foreign to me.)

I assume you already have Oracle Workshop for WebLogic installed and have created a new domain to test our application in. For this application, you will need to download Jersey.

Jersey is the open source (under dual CDDL+GPL license), production quality, JAX-RS (JSR 311) Reference Implementation for building RESTful Web services.

You can download the Jersey archive that contains the jars, core dependencies and JavaDoc. For this example, the only jar files we will be using are

  • asm-3.1.jar
  • jersey-core-1.2.jar
  • jersey-server-1.2.jar
  • jsr311-api-1.1.jar
  • jersey-json-1.2.jar

As you can see, I am using the 1.2 version of Jersey.

The next thing is to create a new Web Service Project in Workshop. Make sure your target runtime is “Oracle WebLogic Server v10.3” and for the Configurations I use “Annotated Web Service Facets JAX-RPC (Recommended) (v 10.3)”. I also checked the “Add project to an EAR” and changed the name to “JerseyJSONEAR”.

Once the application is created, navigate to the source folder and copy the Jersey jar files to the same location. On my computer, the folder location is “D:bea103user_projectsworkspacesdefaultJerseyJSON”. Next we need to add the jar files to the build path of your project. Right click your app folder in Workshop and click on the “Java Build Path”. Click on the “Libraries” tab and then on the “Add External JARs…” button. Navigate to the app folder and select all the Jersey jar files. After clicking ok, you should be able to see the new jars in your Java Resources Libraries folder.

Now we need to modify some xml files to add the Jersey to our project. Open the WebContent folder in you application and then open the WEB-INF folder. You should see the web.xml file in there.



	JerseyJSON

		index.html
		index.htm
		index.jsp
		default.html
		default.htm
		default.jsp
You need to change the code from above to look like the one below.



  JerseyJSON
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  

        35

    
    html  
    text/html  
    
    
    txt  
    text/plain  
  
      
    json    
    application/json    
  

  
        JAX-RS Servlet  
        jersey  
        com.sun.jersey.spi.container.servlet.ServletContainer
          
            com.sun.jersey.config.property.resourceConfigClass  
            com.sun.jersey.api.core.PackagesResourceConfig  
          
          
            com.sun.jersey.config.property.packages  
            com.smart.websvc  
        
        
        	com.sun.jersey.spi.container.ResourceFilters
        	com.sun.jersey.api.container.filter.RolesAllowedResourceFilterFactory
        
        1
  

    
        jersey  
        /*  
    

The only thing that could change from project to project is the param-value for the com.sun.jersey.config.property.packages. This should contain the package name we will configure later in our project.

In that same folder, you need to modify the weblogic.xml file. Change it from



    10.3
    JerseyJSON

to this….



    10.3
    JerseyJSON

    	true

Now we can actually start writing code.

In the Java Resources/src folder in your project, you can create your package. So right click on the src folder and select New, Java Package. For this example I choose to name it “com.websvc”

Then you can create a new class file in that package called JerseyJSON.

Next we will create our test object that will hold the data we want to display. Right click on the src folder again and select New, Java Package. We are going to name this one “com.websvc.vo”.

Then in this package, we will create our test object called TestData_VO.java.

Below is the code that will be in the TestData_VO.java file.

package com.websvc.vo;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class TestData_VO {  

    private int id;
    private String name;  

    public TestData_VO(){  

    }  

    public TestData_VO(int id, String name) {
      this.id=id;
      this.name=name;
    }  

    public void setId(int id) {
        this.id = id;
    }
    public int getId() {
        return id;
    }  

    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}

Now we can go back to the JerseyJSON.java file and change it to match the code below.

package com.websvc;

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

import com.websvc.vo.TestData_VO;

@Path("/JerseyRest")
public class JerseyJSON {

	@GET
	@Produces("application/json")
	public TestData_VO getJerseyREST() {
		return new TestData_VO(1, "Test");
	}
}

The @Path designation is what we want the user to input to request this web service. The @GET means that this will only run for an HTTP GET request. The final one, @Produces, will designate how the data will be displayed.

We now should be able to build our project and deploy to the Oracle WebLogic Server.

Note: In order for me to be able to deploy the application, I had to modify the WebLogic Server startup script to include the Jersey jar files. Look in your domain folder bin directory. You should see the startWebLogic.cmd script. You will need to modify this file. Once you open it up, around line 177, you should see the following command.

echo starting weblogic with Java version:

Below that line you will need to add a new line modifying the CLASSPATH to include the Jersey jar files.

set APPPATH=D:bea103user_projectsworkspacesdefaultJerseyJSON
set CLASSPATH=%APPPATH%jersey-json-1.2.jar;%APPPATH%jersey-server-1.2.jar;%APPPATH%jersey-core-1.2.jar;%APPPATH%asm-3.1.jar;%APPPATH%jsr311-api-1.1.jar;%CLASSPATH%

The APPPATH variable is the location of where your Jersey jar files are.

Now you can start the WebLogic Server and deploy your test app. Once the app has been deployed, you can test by going to

http://localhost:7001/JerseyJSON/JerseyRest/

Your output should look like this…

{“id”:”1″,”name”:”Test”}

This tutorial is very basic. I have been searching around the web trying to do this so that the output is in JSON instead of XML. Once we start moving our applications to the new version of WebLogic, I am thinking of using this more with javascript for some AJAX magic. Any comments, suggestions or questions are welcomed!


4 responses to “Week 20 of 2010”

  1. Hi

    I am creating jax-rs rest webservice using using eclipse and weblogic 8.1

    i followed follwoing steps but still i get 404 error

    could you please help me in this:

    1. New java project
    2. New java class
    3. package com.foo.testrest2.ws;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path(“/hello”)
    public class Hello {
    // This method is called if TEXT_PLAIN is request
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
    return “Hello Jersey”;
    }

    // This method is called if XMLis request
    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
    return “” + ” Hello Jersey” + “”;
    }

    // This method is called if HTML is request
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello() {
    return ” ” + “” + “Hello Jersey” + “”
    + “” + “Hello Jersey” + “” + ” “;
    }
    }

    I added all jersey jars in my WEB-INF/lib

    I added following in my web.xml

    Jersey REST Service
    com.sun.jersey.spi.container.servlet.ServletContainer

    com.sun.jersey.config.property.resourceConfigClass
    com.sun.jersey.api.core.PackagesResourceConfig

    com.sun.jersey.config.property.packages
    com.foo.rest1

    Jersey REST Service
    /rest/*

    And I deployed the application and restarted weblogic server.

    And I tried to access the url http://localhost:7001/testrest2/hello but it gives me 404.

    Am I missing any steps ?

    thanks,
    jyoti

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.