I was wondering how easy it is to create Hello World REST service using Jersey and deploying it to WAS 6.1. Long story short: It is really easy. So here are the steps:
- Download jersey-archive-1.0.2.zip from http://jersey.dev.java.net/.
- Extract jersey-archive-1.0.2.zip.
- Go to your favorite tool for WAS development. Mine was Rational Software Architect 7.5 (RSA) but the same steps works for 7.0.
- Create new Dynamic web project (HelloWorldRest). In RSA 7.5 you should choose WAS 6.1 as your target platform. Also choose Servlet spec 2.4.
- Copy jersey jars from lib folder. That’s the folder where you extracted jersey archive in step 2. Put them in the WEB-INF/lib folder.
- Now you must add Servlet configuration and mapping. You can do this through wizard or you can copy the following in web.xml just after display-name element:
Jersey Servlet ServletContainer ServletContainer com.sun.jersey.spi.container.servlet.ServletContainer ServletContainer /resources/* - Now we can create service implementation class. We are creating simple POJO class (in JAX-RS term Root Resource Class) with one method like one below:
/** * */ package org.example; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; @Path("/helloworld") public class HelloWorldResource { @GET @Produces("text/plain") public String sayHello(@QueryParam("world") String world) { return "Hello " + world; } }
The interesting bits are four annotations:- @Path("/helloworld") – this annotation exposes methods in this class through helloword path. Annotation value is the thing that we will add after resources in URI to call this service
- @GET – annotated method (sayHello) is available through HTTP GET method
- @Produces("text/plain") – method will return plain text
- @QueryParam("world") – with this annotation we are mapping URI parameter world to method parameter world
- Deploy project on server!
- Created a new Java project called HelloWorldRestJava.
- Created new folder called lib.
- Copied jars from WEB-INF/lib to lib folder. Added them to project classpath.
- Created new class with main method:
package org.example; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; public class HelloWorldRestClient { public static void main(String[] args) { Client client = new Client(); WebResource webResource = client.resource("http://localhost:9080/HelloWorldRest"); String response = webResource.path("resources").path("helloworld") .queryParam("world", "World!").get(String.class); System.out.println("Response: " + response); } }
- Run the client as Java application.
Have you a example with glasfish beacause in can't use queryParam
ReplyDeletethak's
christian.vial@ac-lyon.fr
Does it work with any version of Websphere 6.1 ? or is there any fixpack needed ?
ReplyDeleteIt seems that it doesnt't work for WAS 6.1.0.13, with RAD 7.5.3 IDE in my case.
ReplyDeleteI get this:
Could not be defined due to: (com/sun/jersey/spi/container/servlet/ServletContainer) bad major version at offset=6
This is often caused by having a class defined at multiple
locations within the classloader hierarchy. Other potential causes
include compiling against an older or newer version of the class
that has an incompatible method signature.
Dumping the current context classloader hierarchy:
==> indicates defining classloader
==>[0]
com.ibm.ws.classloader.CompoundClassLoader@431c431c
Local ClassPath: C:\workspaceREST\pruebaREST\WebContent\WEB-INF\classes;C:\workspaceREST\pruebaREST\WebContent\WEB-INF\lib\asm-3.1.jar;C:\workspaceREST\pruebaREST\WebContent\WEB-INF\lib\jersey-bundle-1.4.jar;C:\workspaceREST\pruebaREST\WebContent\WEB-INF\lib\jsr311-api-1.1.jar;C:\workspaceREST\pruebaREST\WebContent
Delegation Mode: PARENT_FIRST
[1]
com.ibm.ws.classloader.CompoundClassLoader@3ff03ff0
Local ClassPath:
Delegation Mode: PARENT_FIRST
[2] com.ibm.ws.classloader.ProtectionClassLoader@25462546
[3] com.ibm.ws.bootstrap.ExtClassLoader@27622762
[4] org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader@41864186
[5] sun.misc.Launcher$AppClassLoader@21c021c0
[6] sun.misc.Launcher$ExtClassLoader@3fee3fee
---Original exception---
java.lang.UnsupportedClassVersionError: (com/sun/jersey/spi/container/servlet/ServletContainer) bad major version at offset=6
You probably took the Jersey that is compiled with Java 6. Try to find one compiled with Java 5. Also check that you don't compile using Java 6.
ReplyDeleteI don't get a responce. Don't you need to configure the package of the resource in the XML Servlet section as init-params?
ReplyDeleteSomething like
com.sun.jersey.config.property.packages
packagesResourceConfig
I didn't have no init-params. It has work with web.xml as described.
ReplyDeleteException in thread "main" java.lang.NoSuchMethodError: com/sun/jersey/core/spi/component/ProviderServices.(Lcom/sun/jersey/core/spi/factory/InjectableProviderFactory;Lcom/sun/jersey/core/spi/component/ProviderFactory;Ljava/util/Set;Ljava/util/Set;)
ReplyDeleteIt's about 3hours that I fight with this Exception! I build a webService on Websphere 7, and another client test but always the same exception!
thanks so much, it works fine
ReplyDelete--Ramesh
Thanks alot, It Works fine. If anyone has error, check jar files carefully
ReplyDeleteThanks alot. Followed the instructions and worked like a charm! ;)
ReplyDeleteException in thread "main" java.lang.NoClassDefFoundError: java.util.logging.LogManager (initialization failure)
ReplyDeleteat java.lang.J9VMInternals.initialize(J9VMInternals.java:140)
at java.util.logging.Logger.getLoggerWithRes(Logger.java:337)
at java.util.logging.Logger.getLogger(Logger.java:367)
at com.sun.jersey.api.client.Client.(Client.java:112)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:200)
at com.mits.restclient.HelloWorldRestClient.main(HelloWorldRestClient.java:11)