Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is JAX-RS suitable as a MVC framework?

JAX-RS has some MVC support, but I wonder if JAX-RS is really a good choice to build web application for human use.

If a user enters wrong or incomplete information in a form, it should be displayed again like with Grails or Wicket. Is there a comfortable way to do this with JAX-RS?

As far as I know the URI mapping doesn't work correctly, if not all required parameters are given or there are type conversion problems (with Date for example). Is that correct?

Is there support for internationalized templates?

Here is an example for a simple JAX-RS based GUI application. But it is really simple and thing like i18n and validation are not discussed.

like image 847
deamon Avatar asked Jan 30 '10 14:01

deamon


4 Answers

Yes you can, but you have to clear your head of the old page-post model and start to think of your application as a disconnected UI that communicates with a RESTful SOA. When form data is entered, it post to a service endpoint if the data is not correct then you respond back with an error and the UI handles dealing with that error. You do not post forms to the server in the traditional page-post model but rather you make RPC like calls to your back end system. Your view becomes completely detached from the rest of MVC stack. This makes replacing the view with a custom mobile or IVR system extremely simple.

If a user enters wrong or incomplete information in a form, it should be displayed again like with Grails or Wicket. Is there a comfortable way to do this with JAX-RS?

With a rich internet application you do not have to repopulate data because you never left the page, an XHR call is made to the server and either a success 200 is sent back or an error. The UI then decides what to do based on that response, but the page is still intact because the call was out of band from the main UI thread.

like image 59
kls Avatar answered Oct 16 '22 20:10

kls


JAX-RS is the Java EE RESTful framework. JavaServer Faces (JSF) is the Java EE MVC framework. It supports all what you've mentioned in your question: postback to same form on error, i8n/l10n and much more. To learn more about JSF, go through Java EE 6 tutorial part II chapters 4-9.

You can do a bit MVC with JAX-RS, but it isn't a full fledged MVC framework. The same story goes on that you can do a bit RESTful with JSF, but it isn't a full fledged RESTful framework.

If you want best of both worlds, I think you really need to head to Ruby on Rails or Groovy on Rails.

like image 21
BalusC Avatar answered Oct 16 '22 21:10

BalusC


Or take the integration approach to get the best of both worlds: JAX-RS + MVC.

The JBoss RESTEasy implementation of JAX-RS integrates with Spring MVC. See http://www.jboss.org/resteasy

Here's a little tutorial on RESTEasy + Spring MVC: http://java.dzone.com/articles/resteasy-spring

like image 2
marklai Avatar answered Oct 16 '22 21:10

marklai


There are lots of questions in this one, I'll give my view on two of those.

"I wonder if JAX-RS is really a good choice to build web application for human use."

Web services are usually for machines to interact with, although I would argue it is usually humans that have to programme the interactions - this needs to be compared with SOAP where, at the moment, there is much more scope for machine generated code from WSDLs.

"If a user enters wrong or incomplete information in a form"

  • then in a RESTful HTTP web service which accepts a html form representation you should return HTTP error 400 because the client has provided a representation that does not conform to the representation your service expects - it is up to the client to deal with the error.
like image 1
blank Avatar answered Oct 16 '22 21:10

blank