Saturday, 2 October 2010

REST Web Services in Oracle's JDeveloper in 10 easy steps

I'm currently writing a REST Web Service presentation for the Australian Oracle User Group, and thought it would be useful to document in 10 quick steps how to enable REST Web Services in Oracle's JDeveloper using the Jersey implementation of JAX-RS (JSR311). This is documented in Oracle's online documentation, but that's never stopped me writing a blog entry before in my usual succinct manner.

Step 1 – Create your Application Workspace – base it on the Generic Application template:

...and let it create the default project with no technologies:

...with this result in the Application Navigator:

Step 2 – Download the required Jersey libraries . Note JDev 11.1.1.3.0 that this blog entry is written in supports Jersey 1.1.5.1 and above. At the time of writing the latest non-beta release was 1.4 and is used as a basis for this blog.

From the Jersey download documentation page this requires you to download 3 files:

jersey-server.jar
jersey-core.jar
asm.jar

Alternatively you can use the Jersey bundle, implying you need to download:

jersey-bundle.jar
asm.jar

We'll assume you've downloaded the 3 JARs for the rest of this blog.

Step 3 – locate the JDev Application Workspace on the filesystem, create a subdirectory "lib" and copy the 3 downloaded JARs into this directory:

Step 4 – open the project properties (right click -> Project Properties) in the Application Navigator and add the 3 JARs under the Libraries and Classpaths option – Add JAR/Directory option:

Step 5 – create a simple POJO in the Project, with a simple method to return a String:
public class MySimplePojo {
public String helloRESTWebServices() {
return "helloRESTWebServices";
}
}
Step 6 – add the @Path annotation to the class and add the appropriate import:
import javax.ws.rs.Path;

@Path("myRESTWebService")
public class MySimplePojo {

public String helloRESTWebServices() {
return "helloRESTWebServices";
}
}
Step 7 – Note the orange squiggly line under the @Path annotation. Click on the Path keyword, then select the light globe in the left margin. It will show the option "Configure web.xml for Jersey JAX-RS web services" which when selected will configure the project correctly to run the web service once deployed:

....you'll see the web.xml file added in the Application Navigator:

Step 8 – Annotate the simple method with the @GET and @Produces annotations:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("myRESTWebService")
public class MySimplePojo {

@GET
@Produces("plain/text")
public String helloRESTWebServices() {
return "helloRESTWebServices";
}
}
Step 9 – Save all

Step 10 – Right click the POJO in the Application Navigator and select Test Web Service:

...this will deploy the app to the integrated WebLogic Server, and open the HTTP Analyzer window. From here you can send a request to the deployed REST Web Service and see the result:

And you're done.

In all seriousness step 9 wasn't even much of a step, but "Express REST Web Services in Oracle's JDeveloper in 9 easy steps" doesn't have much of a ring to it.

With Thanks

With my thanks to Gerard Davison for his assistance on sorting out the library details for this post.

2 comments:

Baig said...

Great post ... Indeed its very easy :)

Raj Kumar said...

Thanks a lot
superb :)