What's a JAX-RS implementation? - rest

I have been trying to figure out how to use JAX-RS for quite some time. I started from the very basic concepts and then to gradually understand the annotation styled programming, the meaning of #Path, #Get, #Post, etc.
To my understanding, as to what has been mentioned in a lot of places, JAX-RS is a framework that focuses on applying Java annotations to plain Java objects (Page 27, Bill Burke, RESTful Java).
I have then got confused beyond this point. If JAX-RS in itself is a framework that defines APIs dealing with annotations in order to implement RESTful web service, what's the meaning of "implementation of JAX-RS" such as "Jersey" and "JBoos Resteasy". Another layer on top of JAX-RS? Why do we need them?
Could someone provide me some insights about it? Many thanks!!!

JAX-RS is a standard defined in Java Specification Request 311 (JSR-311) and Jersey / RESTEasy are implementations of it.
Being implementations mean that the spec says "if you put #GET("/foo") on a method (bar()), you may access data X" - now in an app server, someone needs to go in and actually read your deployed code, parse the byte code with the #GET annotation and then if the user actually browses to e.g. http://localhost/foo get this web request to reach bar() and translate the return value of bar() into a http answer (e.g in XML or JSON representation).
So the spec with the name "JAX-RS" only provides the syntax and semantics of e.g. #GET, but the work of parsing requests, calling the right methods, marshalling the result values etc. need to be done by a package that implements the Spec.
Work on the version 2.0 of the standard has started as JRS-339.
See also http://en.wikipedia.org/wiki/Jax-rs

JAX-RS is a specification for RESTful Web Services with Java. There is a reference implementation that is included in Java EE but since it is a specification, other frameworks can be written to implement the spec, and that includes Jersey, Resteasy, and others.

Related

What is the difference between org.apache.http.HttpRequest and javax.servlet.http.HttpServletRequest?

My research so far says that javax.servlet.http.HttpServletRequest is the interface to calling regular Java Servlets while org.apache.http.HttpRequest is typically used to implement RESTful services. I see an example for the same in one of the internally available frameworks in my organization where org.apache.http.HttpRequest is the interface to program RESTful services.
I still feel that org.apache.http.HttpRequest has been made available by Apache to facilitate RESTful implementation since this interface does not have any status code and works with passing entities as responses.
What exactly is the difference between the two interfaces and when one should be used over the other?
HttpServletRequest is a server-side class that is part of the Java EE Servlet APIs. You use it when you are implementing ... a servlet.
In the Java context, HttpRequest could (in theory) be anything ... because it is not a Java SE or EE class. But usually it is a class in the Apache Http Components library. This is typically used for client-side code, though it is also possible to use it server-side too.
(There are HttpRequest classes in non-Java contexts also ...)
What exactly is the difference between the two interfaces and when one should be used over the other?
They are unrelated interfaces. (Or "exactly" unrelated ... if you prefer :-) )
Use HttpServletRequest when you are implementing servlets.
Don't use HttpRequest when you are implementing servlets.
"RESTful" is orthogonal; i.e. you can implement RESTful servers using servlet, and non-RESTful servers without using servlets.
I am still not clear about the basic difference between the two. Why would somebody need a HttpRequest in the first place if HttpServletRequest is already there?
Because that somebody's application may not be using the standard Java EE servlet framework. And if they are not, then it is not "already there".
From this point of view, the basic difference between HttpRequest and HttpServletRequest is that they are part of different frameworks, and you use one or the other depending on which framework you are using.
Why do we have two classes? Because of history. Java EE servlets came first, and were standardized many years ago and are widely used. The Apache HTTP Components library was implemented later to address use-cases that servlets did not address; e.g. where servlets are too heavy-weight.
Oracle can't change Java EE to replace HttpServletRequest with the Apache HttpRequest class because it would break too much customer code.
Apache couldn't have adopted HttpServletRequest in HTTP Components because it has "baggage" that is not appropriate to non-servlet use-cases.
Either way, it is what it is.
Which framework do you choose? How do you choose? Those questions are both off-topic for StackOverflow. (Recommendations, subjective, too broad, etc)
i think the basic difference is Httpservletrequest is part of communication between container and servlet as container creates the object for it and passes it on to servlet,while as Httprequest is part of communication between container and client because container converts Httpservlet respone into Httpresponse and then sends it back to client.

what exactly is JAX-RS?

I am learning the REST architecture these days. I have developed many small small projetcs using jersey. My question is what exaclty is JAX_RS ? I understand that JAX-RS is a set of interfaces (all the javax.ws.rs.* classes) which contains annotations like GET,POST and a lot more. Jersey is one of the bundle which has classes which implement those methods. My doubt is if we download java , do the JAX_RS interfaces come with that ? I have used maven to build REST. There i have seen a lot of entries being added to pom which does my job, but i want to get the actual feel of what exactly is this JAX-RS. Any help is appreciated ! sorry if this is foolish !
JAX-RS is just a specification. A specification specifies how such a framework should work (in a PDF file), and provides different Java interfaces and annotations and enums (like javax.ws.rs.* classes), but no functional code.
The URL for JAX-RS specification are:
JAX-RS 1.1 (you probably use this one) https://jcp.org/en/jsr/detail?id=311
JAX-RS 2.0 https://jcp.org/en/jsr/detail?id=339
Along with the specification there are implementations of that specification, which is functional code (java jars), that you can use in your applications. Such implementations are JERSEY and RestEasy (usually applications use only one).
Now what you need is a JAX-RS tutorial. Take the first two results from google:
http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html (I find this one very good)
http://www.vogella.com/tutorials/REST/article.html (this one is very comprehensive, IMHO)

Is it possible to have a class support both a JAX-RS service and JAX-WS service?

I've created a RESTful web service using jersey and JAX-RS annotations. It's also documented using enunciate and looks great. However, SOAP support has been requested as an option. I noticed in this outdated enunciate example JAX-WS and JAX-RS annotations in the same class. Is this possible? I've tried it myself and enunciate generates documentation correctly, but the services don't actually work.
I'd prefer to have the exact same class support both interfaces rather than two separate classes (one soap one rest) pointing to the business logic class. This would prevent possibly having code in two places.
Here's the example on outdated software versions:
http://docs.codehaus.org/display/ENUNCIATE/A+Rich+Web+service+API+for+Spring
I'm using
Jersey 1.8
Spring 3.0.5
Weblogic 11g
Thanks!
/Chip
I'm not sure what might not be working, but a lot of the Enunciate example modules use both the SOAP and REST annotations on the same class.
Here's one for Jersey/JAX-WS.
Here's one for JBoss WS/RestEasy.
Here's one for CXF.
We ended up making a separate service for SOAP than the REST service. We also found it best to have interfaces for each that enunciate could generate from. This way we could control what documentation it generated. It also started functioning smoother. Still having a problem with the namespaces though as they're all default and ns0 is generated but enunciate links are to ns2/3/4/5/etc. So many links are broken.

For RESTful services in Java, is JAX-RS better than an MVC framework like Swing, Grails or Play?

For example, Play-framework supports RESTful services like this: RESTful on Play! framework
How does this compare to something like Jax-RS Jersey implementation? Does a framework like Play run circles around Jersey because of all it's cool bells and whistles, and it does REST too?
Developer productivity is important, but so is a proper implementation. Perhaps using an MVC framework for REST only services is 'wrong'?
Note, only RESTful services, no UI components at all.
Even though it's not "wrong" to use an MVC framework for RESTful services, there are some pros and cons versus using a JAX-RS implementation.
(Disclaimer: I have only used Jersey and Play! for fun, and not on production-grade systems, so I have tailored my comments more generally to MVC vs. JAX-RS. Keep in mind that these are broad generalizations.)
MVC frameworks--at least the ones that are considered developer friendly and "slick"--typically save you from having to build a persistence layer (the model part). Most also simplify "routing" requests using either scaffolding via convention or some form of configuration. The downsides are that you have to conform to some conventions for your controllers and usually have to write a view for each resource (or build layers of abstractions to avoid rewriting the same code).
JAX-RS excels at defining the routing (using Java annotations) as well as eliminating any restrictions on the service class. In my experience, that has greatly reduced the amount of boilerplate code and developer overhead. Jersey and Apache CXF also handle the XML or JSON serialization using JAXB annotations, which eliminates the need to figure out the view in an MVC context. The downside here is that you have to figure out your own ORM or persistence layer, which could be good or bad depending on whether you're building on top of existing data or creating a greenfield system (or using something other than an JPA/RDBMS e.g. NoSQL data store).
My own personal comment: Play! is a really cool framework, but I'd choose CXF (or Jersey) over an MVC framework any day for building out a RESTful service. In my experience, this frees up the developer to focus on the logic needed for the service, and opens up options for different database approaches. Right tool for the right job.
As a rule of thumb: For Scala, use Play. For Java, use Jersey.
You can use Jersey/Scala and Play/Java; I've done both. It works. It isn't bad. But unless you have a particular reason to do that, I wouldn't mix ecosystems. Java and Scala are interoperable but they have different ecosystems, I would avoid adding Java-isms if you are using Scala or Scala-isms and dependencies if you are running straight Java.
Jersey and Play are generally close for REST services. Neither really has any killer features over the other.
Jersey defines URL mappings in annotations, Play defines them in a service wide route file. And they bundle or have varying quality of integration with different libraries for things like XML, JSON, database, testing, mocking, dependency injection libraries and app server deployment.
The Java world has JMS, Spring, JUnit, jdbi/hibernate/jpa, Jetty/Grizzly. The Scala world has Akka, specs2/ScalaTest, Anorm/slick. Jersey is a better fit for the first world, Scala for the second. You can definitely cross that, but it will be a little less elegant and might require more glue coding.
JAX-RS is a standard and implementations can be created by different vendors. Jersey is one such implementation. The other frameworks may make use of JAX-RS but are not standards. So it is not a one-to-one comparison.
I have never heard of Play before but it does look interesting, more akin to Rails and Django than Jersey. What I like about Jersey is that it can be integrated into existing Java web applications by simply adding the JARs and declaring some things in the web.xml. What I find confusing about Jersey and JAX-RS is the routing.
Play seems to make routing easier, however, correct me if I'm wrong, seems like it is an all-or-nothing framework and cannot be used alongside other servlets in the same web application.

client server semantic data transfer with GWT

In short, how do you transfer semantic data between client and server with GWT and which frameworks do you use? Read on for more details that I've thought about.
For example, using GWT 2.2.0 features like the RequestFactory will bring the constraint to have java beans transferred while the semantic resources are represented as triples and a resource can have a varying set of properties. So the RequestFactory itself cannot be shaped to transfer semantic-driven data easily.
A way to do that would be to use RequestFactory with beans that represent triples. Such bean would have 3 properties: subject, predicate, object. These beans will be transferred to client which will know to query, change their properties and then send them to server. This approach will however need a custom implementation(there are no GWT-based frameworks to represent semantic data on client-side, from what I've searched so far) and that could prove buggy or unoptimized. I've seen this approach in this project: http://code.google.com/p/gwt-odb-ui/ - it used GWT-RPC and implements some classes that represent semantic resources. However, I think it's in an incipient stage so I'm reluctant to copy their model.
Also, I've found that Restlets is a framework that supports the semantic web approach to applications. However, there is no documentation or an example on how to use Restlets with Semantic Web and perhaps with GWT. Also, Restlets is also supporting GWT. Does anyone know if this is a viable solution or not?
Thank you!
Restlet should work quite well for you. It has a GWT edition able to automatically serialize your triple beans. In addition, it also comes with an org.restlet.ext.rdf extension, including a Link class similar to your triple bean idea.
For further documentation, I would suggest the "Restlet in Action" book which covers GWT and the semantic web from a Restlet and REST point of view.