开发者

Is JAX-RS suitable as a MVC framework?

开发者 https://www.devze.com 2022-12-18 19:13 出处:网络
JAX-RS has some MVC support, but I wonder if JAX-RS is really a good choice to build web application for huma开发者_Python百科n use.

JAX-RS has some MVC support, but I wonder if JAX-RS is really a good choice to build web application for huma开发者_Python百科n 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.


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.


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.


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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消