Is JAX-RS suitable as a MVC framework?
Asked Answered
M

6

15

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.

Marmolada answered 30/1, 2010 at 14:29 Comment(0)
L
5

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.

Limburg answered 17/6, 2010 at 2:24 Comment(4)
That implies the usage of JavaScript for almost all communication with the server, right?Marmolada
Yes, that is one of the core concept, the server consist of two parts, the web server which provides static HTML and the application server which is a series of REST endpoints for data and business logic. From a browser you make XHR calls for getting data and for form processing, business logic. The nice part is that this gives you very fine grained control. For example you may want to validate a form after all information is entered but you may also want to check to see if a username is taken at the point that they enter it. By doing both in XHR /JAX-RS calls you have an common architecture.Limburg
To clarify even thought the server consists of two parts they physically can be the same server it just helps to visualize them as two distinct entities. As well, I personal use a CMS that can export static HTML to the web server. This provides templating with no run-time dependencies. I do not use JSP but rather build the front end with pure HTML, CSS, and JavaScript.Limburg
+1 The key here is that your JAX-RS resources are used not only as a RESTful API but also as your Ajax callback endpoints. Continue the REST paradigm by replying with a UI representation of the data when the accept headers include "text/html" and pure XML or JSON data if "application/xml" or "application/json", respectively.Kyne
P
3

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.

Please answered 30/1, 2010 at 14:38 Comment(3)
Thanks. I know JSF, but I'm looking for something RESTful to build GUI apps.Marmolada
Have a look for Ruby or Groovy on Rails.Please
Grails is Ruby on Rails inspired, but not a clone. My two cents, BalusC :)Whereto
W
2

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

Whacking answered 4/2, 2010 at 6:55 Comment(1)
Thank you for sharing the link to this interesting article!Marmolada
S
1

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.
Subfamily answered 30/1, 2010 at 16:31 Comment(2)
I agree with returning a 400 status when the send incorrect data. But that alone wouldn't be helpful when the data was entered in a form. It would be much better to highlight form fields.Marmolada
jeez, what do you think is happening behind the scenes in rails? you have multiple times stated that you want a RESTful way to do things. I think you need to read up on what that means: en.wikipedia.org/wiki/…Whereto
K
1

Take a look at ReXSL - it's an MVC framework, on top of JAX-RS. Thus, the answer is - yes, JAX-RS is perfectly suitable for MVC design.

Keffiyeh answered 12/5, 2012 at 10:50 Comment(0)
M
0

Short answer: YES.

It serves as the base to implement MVC (MVC 1.0 - JSR 371) for JAVA EE 8. The Controller will be a JAX-RS bean with @Controller annotation (on class or methods).

For mor information see: MVC 1.0 (JSR 371)

If you took that path at your time you've done a really good decision, it should make it easier for you to upgrade and use the new Java EE 8 MVC architecture.

Mitinger answered 29/4, 2015 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.