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.
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.
Related
I am considering moving from server-side rendering view technologies like JSP, Struts to client-side rendering view technologies using AngularJS,
A popular Javascript framework for modern browsers.
When we are making this change and what are all the things that I may encounter.
If anybody who are experienced in Spring Web MVC and JSP development and would know how Spring MVC can work together with a client-side Javascript like AngularJS.
Kindly Answer.
You have to rewrite the view part using angularJS, and replace the logic from the server with a REST API.
It's often a huge work, and the hardest part is to migrate the existing server-side session management to the browser, because a REST API is stateless.
Below is one of the suggested way in which i recently migrated my existing spring MVC web-application in Single page application using AngularJs as client side java-script framework:
1).First you needs to bisect your core business logic which must needs to handle in DB layer,if not done already. i.e. SP's,trigger's,etc...
2).Then for the thing mentioned as hardest by Toilal ("to migrate the existing server-side session management to the browser, because a REST API is stateless"), you can consider Spring Boot as one of the options to build things quickly instead of Spring MVC with fusion of java8 features to yield the JSON response via API's.
3).And than last but not the least, build UI layer with AngularJS in front End encapsulating the data returned by the spring boot API's. Please note that as angular is super power full, you can do most of your business logic at client side,your DB layer just needs to give you the whole bunch of business data to render the screen,other things you can easily handle in angular.
Thus, this way by making the DB layer thin and imbibing angular at front end, you can improve performance of your existing application drastically.
You can also check this AngularJS with Spring-mvc useful thread in same context.
I've 2 questions about Java Rest APIs
1'st:
I would like to create a Rest API with Java Servlets for using in the mobile applications(IOS and Android)
and before doing that I would like to clarify the rest api content. However as much as I search through google all I found was automatic API creators from your Java code. But what I want is different first I want to document our Json Rest API so mobile and server developers can start to work independently.
Do you know any tool for that?
2'nd:
What I want to achive in our java server application is simple. Get simple json requests from mobile clients and do some database query and respond back with simple json objects.
For achiving this do I need to use any additional Rest API framework such as spring mvc (or something else) or just using Java Servlets and parse the request in doPost method and respond it there
Which one do you suggest?
Thanks
Restlet Studio (http://restlet.com/technical-resources/restlet-studio) or APISpark (http://restlet.com/products/apispark/) can bring you what you expect.
Restlet Studio allows you to define / design you API with a Web IDE: resources and their methods, representations (exchanged data structures). You can then have access to corresponding Swagger content and generate online client SDKs and even server skeletons for your API (this is internally based on Swagger Codegen)...
To implement your RESTful applications, Restlet can help you. It's a Java REST framework to access and / or implement RESTful applications. Restlet can be used within a servlet container with its servlet extension (see this link https://github.com/restlet/restlet-tutorial/blob/master/modules/org.restlet.tutorial.markdown/02_Server_Side/04_Server_Deployment/02_Servlet_Deployment.md).
Hope it helps you.
Thierry
I aggree with Stephan comment.
With Spring Boot and Spring Data Rest, the only code you will need to write is the mapping between your DB and your DAO Entities.
To document your API before writing it, you can use in fact any regular tools.
REST APIs don't have anything specific compared to others APIs to be documented.
Much of the documentation of APIs I've written were using Ms-Word...
Swagger provide a Json syntax to document a REST API, but it is mainly useful when used with Swagger-Ui, which allow to request the deployed server dynamically. You can use Swagger for your documentation, but as there will be no already existing server, I'm not sure it is worth the cost of learning its syntax. The main benefits is to give you constraints (like predefined fields) to follow.
By the way, I think that writing a REST Api Mapping or just document it takes the same amount of time, so I'm not sure it is really worth making all the documentation in a stand-alone way.
im using spring MVC and webflow to create a game server and serve some web pages to the users. Thing is, the javascript game will also make multiple ajax calls to restful services on the same server for some game logic. While the web page serving performance is not critical, the restful service calls need to be as efficient as possible (efficient as in response time).
For performance of the services, would it be better to use pure JAX-RS (jersey) web service calls without the spring ws overhead (since i understand the spring layer could affect performances negatively) or would it be the same if i used the spring webservices framework instead and maintain integration with the rest of the spring family?
thanks!
There aren't many clear benchmarks out there, but take a look here:
http://www.techempower.com/benchmarks/
It clearly shows the overhead of using Spring. Compared to Servlets that serve JSON manually Spring is "slower". Personally, I don't think that Spring cuts development time, unless you are very much familiar with it. Creating a simple servlet that will act as a REST API is very simple. Take a look at the servlet code from the benchmark:
Servlet benchmark
I don't think Spring per se will affect performance negatively. Where did you hear that?
Spring web services are "contract first" SOAP services. If you're saying that you don't want to pay the overhead of SOAP, XML, marshalling and unmarshalling, then you have a valid point. That's true whether you use Spring to implement the services or not.
REST is HTTP, so it's a natural for AJAX calls.
I believe Spring 3.0 supports REST, so it's not a "Spring or not Spring" choice:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/new-in-3.html#d0e1188
I would like to use RESTful services in a CDI/JSF2 application. I am not very familiar with JAX-RS however I have read that its lifecycle does not play well with CDI/JSF2. Is it possible to incorporate JAX-RS with CDI/JSF2 in a JEE6 stack? If not are there alternatives?
thanks
You can use JAX-RS just fine along side a JSF application, however, they don't play well together. For instance, you can't use JAX-RS to have "http://localhost:8080/story/92/" return a JSF page with a Story entity with ID 92.
You can, however, use PrettyFaces to do something very similar to this.
JAX-RS makes sense for non-JSF resources. "http://localhost:8080/story.xml" makes perfect sense for JAX-RS.
You can call your rest services from your JSF templates. For example you can run your rest services from http://localhost:8080/services/ while your JSF page is returned on http://localhost:8080/index.xhtml. You can then write some custom javascript to call your rest services from your JSF page. This is actually the general practice with REST services so they can be consumed by many different UIs. JSF may not be the best for consuming REST services, but it is possible.
I'm creating a prototype for a java web application.
Frontend is a Swing-based java applet.
Backend should be a type of web-service, that is called by applet.
Backend should run inside a servlet container and should have its own security (username/password) database. I know, that Tomcat has its own user database (realm), but the app should have own. Web-services, in turn, carrying out app logic and database access (via Hibernate).
I'm a newbie for a web development and I'm getting lost in a huge amount of the java web frameworks. Even just reading 'introduction' and 'getting started' documents takes a lot of time.
So I need an advice which framework(s) are suitable for the task and not very complex for a quick start.
Thank you
I would avoid any web framework in such case. Most frameworks are designed so that it is easier to connect bussines logic (backend) with WWW user interface. In your case you don't need web GUI - you have an applet, so web framework like Stripes, Struts, etc. would not help to much.
I think you can use servlet or several servlets as a connector between backend and you applet. Servlets are simple, easy to learn.
If you want to have some abstraction layer with additional services, like security, for instance, you can consider Spring Framework, but it has its own learning curve.
Spring (http://www.springsource.org/) seems like a good choice to handle DB access, and server side logic. Spring-security can be used to integrate security (it takes some time to get started, but it works very well). SpringMVC can be used to output simple XML documents, or if you need more complex remoting capabilities, SpringRemoting is a good solution.
If you want to go the full WebService (with a capital W and a capital S) Spinr-WS can be useful.