This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Web Services and the fly likes Getting started with ReST Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Getting started with ReST" Watch "Getting started with ReST" New topic
Author

Getting started with ReST

akhil achuthan
Ranch Hand

Joined: Mar 29, 2006
Posts: 69
hi,
i m using jboss5. Am in need of having a ReST implementation in my jb5 server. Be exact i need to have server methods like addUser, getUserName, deleteUser etc as my ReST methods that will access and get the data from other ejb session beans in my application and return the value.

Can somebody please give a sample code for the server part. How to i code a server session bean to make it as ReSTfull WS class? and how do i access the methods using ReST?


thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41634
    
  55
REST is tightly linked to HTTP, so to access a RESTful service you can use any HTTP client API, such as the URLConnection class or the Apache HttpClient library.

As of JEE 6, session beans can be exposed as REST resources. See http://blogs.sun.com/kensaks/entry/jax_rs_and_ejb and http://blogs.sun.com/sandoz/entry/glassfish_v3_ejb_3_1 for more information.


Ping & DNS - my free Android networking tools app
akhil achuthan
Ranch Hand

Joined: Mar 29, 2006
Posts: 69
thanks for the reply..

i checked out the link specified.. the jboss that i use 5.1.0 does not seem to support no-interface session beans..

Getting bk, the following is what i did- just for info I have no idea as to how to deploy a ReST application

1. i tried creating a session bean just like mentioned - code below (along with a remote interface) :

@Stateless(name = "RestServer")
@Path("myPath")
public class RestServer implements RestRemote
{
@Context private UriInfo info;

@GET
public String get()
{
System.out.println("INSIDE GET METHOD ----------------------------------");
return "this is a test";
}
}

2. Bundled the session bean into a jar - restserver.jar and put it in jboss server/default/deploy folder, and started jboss.
3. Jboss started normally, the restserver.jar got deployed as a normal session bean, but what about the ReST. I see nothing for webservices or REST in my jboss ..
4. How do i access the method get() through a browser by passing a url? i tried many combinations, but failed

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41634
    
  55
JBoss 5 supports JEE 5, which doesn't include the JAX-RS API. You'll have to add that yourself to your web app; the reference implementation is called Jersey (on dev.java.net). After adding something like this to web.xml:

REST resources should be accessible via http://your.server.com/yourWebApp/rest/myPath/get

Note that Jersey requires that resource class names end in "...Resource".
akhil achuthan
Ranch Hand

Joined: Mar 29, 2006
Posts: 69
somethinz happening strange... i get an exception java.lang.NoClassDefFoundError: javax/ws/rs/core/HttpContext while starting jboss. I see that this class is available with jsr311-api-0.3.jar, but not with any versions above 0.4 or so.

The version jars either contains javax/ws/rs/core/HttpContext or javax/ws/rs/core/Request. Either case i get exception for the other class not found....

confused........

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41634
    
  55
That sounds as if there may a version mismatch between the various jar files needed to use Jersey. Make sure that you download all jar files that go with a particular Jersey version (preferebly 1.1.4.1 these days), and that you remove all others.
akhil achuthan
Ranch Hand

Joined: Mar 29, 2006
Posts: 69
thanks.... its much much better now....

i could access the server method with the url. but the response is failing.. seeing a long list of exceptions too... probably looks like jar version issues..

17:17:55,328 INFO [STDOUT] INSIDE GET METHOD ----------------------------------
17:17:55,328 INFO [ProviderFactoryImpl] Searching for providers that implement: interface javax.ws.rs.ext.MessageBodyWriter
17:17:55,343 INFO [ProviderFactoryImpl] Provider found: class com.sun.jersey.impl.provider.entity.StringProvider
17:17:55,343 INFO [ProviderFactoryImpl] Provider found: class com.sun.jersey.impl.provider.entity.ByteArrayProvider
17:17:55,343 INFO [ProviderFactoryImpl] Provider found: class com.sun.jersey.impl.provider.entity.FileProvider
17:17:55,343 INFO [ProviderFactoryImpl] Provider found: class com.sun.jersey.impl.provider.entity.InputStreamProvider
17:17:55,343 INFO [ProviderFactoryImpl] Provider found: class com.sun.jersey.impl.provider.entity.DataSourceProvider
17:17:55,343 INFO [ProviderFactoryImpl] Provider found: class com.sun.jersey.impl.provider.entity.MimeMultipartProvider
17:17:55,343 INFO [ProviderFactoryImpl] Provider found: class com.sun.jersey.impl.provider.entity.FormProvider
17:17:55,343 INFO [ProviderFactoryImpl] Provider found: class com.sun.jersey.impl.provider.entity.FormMultivaluedMapProvider
17:17:55,343 ERROR [[JerseyWebApplication]] Servlet.service() for servlet JerseyWebApplication threw exception
java.lang.InstantiationException: com.sun.jersey.impl.provider.entity.XMLRootElementProvider$App
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at com.sun.ws.rest.spi.service.ServiceFinder$LazyIterator.next(ServiceFinder.java:450)
at com.sun.ws.rest.impl.provider.ProviderFactoryImpl.cacheProviderMap(ProviderFactoryImpl.java:109)
at com.sun.ws.rest.impl.provider.ProviderFactoryImpl.createMessageBodyWriter(ProviderFactoryImpl.java:173)
at com.sun.ws.rest.spi.container.AbstractContainerResponse.writeEntity(AbstractContainerResponse.java:245)
at com.sun.ws.rest.spi.container.AbstractContainerResponse.writeEntity(AbstractContainerResponse.java:223)
at com.sun.ws.rest.impl.container.servlet.HttpResponseAdaptor.commitAll(HttpResponseAdaptor.java:125)
at com.sun.ws.rest.spi.container.servlet.ServletContainer.service(ServletContainer.java:134)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
akhil achuthan
Ranch Hand

Joined: Mar 29, 2006
Posts: 69
im into a ne problem now.. i need to pass an argument value to my ReST method through my URL. But whatever i pass the server does not receive it.


@Path("/inventory") // sets the path for this service
public class InventoryResource
{
@GET // This method will process GET
// requests to the @Path value set above
@ProduceMime("text/html") // content type to output
@Path("/deviceInventory/{id}")
public Appliance getDeviceInventory(@QueryParam("id") int id)
{
System.out.println(">>>>>>>>>>>>>>> FETCHING INVENTORY <<<<<<<<<<<<<<<<<<<<<<<<< : "+id);
Appliance app = new Appliance();
//fetch the details and set it into app object.
return app;
}
}

i tried all the foll urls
http://localhost:8080/MyRestServer/rest/inventory/deviceInventory/id=25
http://localhost:8080/MyRestServer/rest/inventory/deviceInventory/25
http://localhost:8080/MyRestServer/rest/inventory/deviceInventory/id/25

but in all the case i get the server print as
>>>>>>>>>>>>>>> FETCHING INVENTORY <<<<<<<<<<<<<<<<<<<<<<<<< : 0
 
 
subject: Getting started with ReST