I have a small legacy application which is created using the old servlet style approach wherein
it extends HTTPServlet. The URLS are mapped with query parameters
http://localhost:8080/MyApp/servlet/ReportServlet?id=CA
Now they wanted it to be rewritten and follow REST Urls style.
http://localhost8080/MyApp/servlet/ReportServlet/id/CA
I dont know much how this is done so I'd like to ask for guidance on how to create a web application
with REST urls.
Most of the servlet tutorials uses annotations but never speaks about creating RESTFUL URL
I have searched SO but cant find similar questions so feel free to closed this if this is a duplicate but kindly
point me to a resource that teaches this.
Note that I am not looking into creating a RESTFul web service but how to create a Session Based Web Applications
using HTTPServlet.
I am not to keen on using any specific frameworks so just with the bare JEE stack.
Have a look to this excellent tutorial to get started: REST with Java (JAX-RS) using Jersey
Then read this book: RESTful Java with JAX-RS
It helps me a lot starting fast : clean examples, real life examples, good pratices ...
It presents how the JAX-RS specification works in depth. It also presents you some implementations: Jersey, RESTEasy etc.
If you want to used servlet is rest end point see this article http://riddhimajava.blogspot.in/2014/07/how-to-use-servlet-as-rest-end-point.html
I think this blog entry give a good practices and highlights about REST-API: http://blog.2partsmagic.com/restful-uri-design/
(I have tons of articles that I read, I can post some more, but this is good due it is technological agnostic, which I mean it doesn't concert to JAX-RS or an implementation. I would also recommend some cool presentations that you can find in slideshare ).
Related
I have created a simple Web Portal using JSP/Servlets. It is connected to MySQL Database and basically the portal is just used to manage the records in the MySQL Database.
I now want to expose REST APIs for anyone to retrieve and insert information into the MySQL database.
Would I be able to do so using JSP/Servlets.
It would be great to have suggestions.
Lets first get some short definitions:
REST is an architecture design. You can read this clear explanation from Wikipedia. Today most REST services use JSON as a message format, but this is not mandatory neighter part of REST. You can have XML, images, etc.
Servlet is a RESTful implementation. You can extend HttpServlet in Java an implement the RESTful methods (GET, PUT, POST, DELETE). Lot of people just go with POST and GET, so it is not RESTful by definition, but it do the work.
JSP is not what you are looking for in this case. Its mostly designed for user interface, and is not the right approach to expose "API"s as services and such.
Now about your question, the answer is yes you can use Servlets, but there are better tools to implement REST today. You'll have a litte more work to set up your structure but it will be an elegant solution.
The way to go with Java is with JAX-RS, the Java Enterprise specification for RESTful Services. You can follow the official tutorial Building RESTful Web Services with JAX-RS. I highly recommend you to follow this tutorial since it will give you a good grasp over the concepts involved.
You can use Spring too: Building a RESTful Web Service. I use Jackson 2.0 with Spring and it works like a charm.
Other example here.
Yes, you can do this only with servlets, but this is not a great idea, because there are a lot of good frameworks to do this.
Take a look a this link Jersey
I don't know "anything" about WebServices in Java and eventually I need to do some REST work in Java and well I want to strart learning it this weekedn d:) So my question is do I need to first learn Java web services before I can learn REST webservices? what are the prerequisites stack of knowledge I need to learn first? and your suggested resources.
Thanks
The Java Web Services stuff only have a small relationship with REST services.
Both are "remote services" for the web, so they share the same kind of problems: object representation, security, error handling, etc.
But you don't need to learn the complexities of WS-* like service definitions (WSDL), SOAP... and a large list of stuff.
The small relationship between REST and WS in Java, comes when you want to "automatically" convert objects to XML or JSON. Because Java REST frameworks based on JAX-RS can do that by using JAXB (Java XML Bindings) an object conversion framework used also in the Java WS framework. But you don't need to use that object conversion stuff either.
Some JAX-RS compatible frameworks -like Apache CXF-, can be used also to do JAX-WS (the Java standard API for WebServices). But note that general services frameworks like CXF, have to deal with other complexities (like support for different transports) that you don't need to learn.
What you need to learn first?
Java Servlets: You don't need to be an expert on servlets, but REST services are mounted over the Java servlets infrastructure and knowledge about it will help.
Dependency injection (DI): When you start doing REST resources that access to databases or other services, you'll want to pass some collaborator objects to do that. Most of the frameworks resolves that problem by using DI. For example most of the JAX-RS frameworks can be used with Spring or Guice.
TIP: I found lots of people that uses Spring or Guice because.. tutorials shows that is the correct way to do it. But they don't know what are trying to resolve, and end with complex unmaintainable stuff. My recommendation is that before using any DI framework, first learn the concepts behind them.
(Optional) A little bit of JavaScript and AJAX. If you plan to use the REST services from a web page, is good to learn how they are going to be used.
Edited: To include a mention to Apache CXF, and use the term JAX-RS instead of Jersey that is a specific implementation.
There's a good summary of the Richardson book here: http://martinfowler.com/articles/richardsonMaturityModel.html,
I've been looking into JAX-RS lately because I really like the Java platform and a RESTful style of web development. I've read a lot about using JAX-RS for creating RESTful Web Services, but is it possible to use JAX-RS to create RESTful web sites? I guess more specifically, is it possible to use JAX-RS as a controller (to retrieve required data from the server) and then forward control to a view engine to render the appropriate HTML?
I've been googling around but haven't found any resources that show you how to do this.
Thanks for any insight/help.
I think you are having difficulty with your google searches because not many people are doing this. JAX-RS was designed for web services, so developers don't think of using it as a controller for web applications. However, there is no reason it wouldn't work.
Check out this blog post: JAX-RS as the one Java web framework to rule them all?
I think it's exactly the kind of thing you are looking for.
If you truly want to rely just only JAX-RS for your web framework, Jersey might be your best bet. Keep in mind the features you get from it are going to be bare minimum and you are obviously not going to get all the bells and whistles like what's provided by JSF, Wicket, etc.
If you know your web application is going to rely on Spring, perhaps you should consider using Spring MVC 3.0. It provides restful web services-alike and it gives you better features so that you don't need to implement most of them yourself. Granted, Spring MVC 3.0 is not an implementation of JAX-RS and based on what the Spring developer said, it seems like they will never make Spring MVC as an implementation of JAX-RS since they are already quite a few stable implementations out there. However, the syntax is pretty similar in my opinion, or at least I was able to understand them rather quickly even though I have been using Jersey for quite awhile.
dbyrne is right that almost no one is doing this. It's more conventional to use JAX-RS to dump information to JSON or XML. Then you fancy up the web browser with an RIA framework (e.g. Ext JS), which handles manipulating the DOM and injecting data as its fetched in JSON/XML form. This approach is powerful. You can write multiple, possibly non-browser clients for the service, all parsing the same JSON/XML. You can write "one-page" webapps, where all information exchange happens through AJAX after the initial pageload. I urge you to investigate and consider its strengths and weaknesses in the context of your particular problem.
Returning to your question: the answer is "sort of". This functionality is not directly provided by the JAX-RS spec (as of 1.1). However, it is in the JAX-RS reference implementation, Jersey, through the Viewable response object. See this blog post if you want to investigate further: http://blogs.oracle.com/sandoz/entry/mvcj. I want to point out that I have no experience with this side of Jersey. It has been pleasant to write XML/JSON-returning web services with Jersey, but I can't speak to this server-side HTML templating business.
Edit: dbyrne's edited his answer to include a blog post which points to the one mentioned above. I think we've both converged on approximately the same answer.
I just recently started with object oriented programming, using java. Before I was programming in old visual basic which wasn't object oriented and in old php which wasn't object oriented ..
Now my question : Where can I learn about webservices and object oriented programming through concrete examples? (real life examples not hello world type)
I read all of the articles which I could find on wikipedia, google etc. Does anyone have 1st hand expirience from switching to object-oriented programming , of course I'm more interested in success stories :D
Thank you
You should treat learning about webservices (and what exactly do you mean by this?) and OO as two seperate subjects. Both are orthogonal to each other. Learning object orientation is independent of learning how to deliver services over the internet.
With regards to learning OO this might help.
With regards to web services - you'll need to provide a bit more colour - are you looking to develop web applications? If you are looking to develop a webapp there are a bewildering array of java web frameworks to chose from (wicket, tapestry, spring, struts, grails, aribaweb, the list just goes on and on). If you're just starting out with Java & OO you might overwhelm yourself by diving straight in to a webapp framework. I'd suggest learn some Java & OO first, then play around with the Java Servlets/JSP pages, which should give you the basics, and finally pick a framework like grails or tapestry to learn.
If you don't need a webapp and your just accessing/exposing another service over the web then you have most of what you need for network IO in the core J2SE stack, with the addition of possibly some XML/JSON parsing libraries. Java also has a full blown Web Services API - but it's may well be overkill for your needs. You could implement a simple http webservice by simply using Tomcat and Servlets and reading/writing the request response as xml/json.
The two are entirely different beasts. I would suggest looking at existing frameworks that help you implement web services with Java. For instance you can look at the Spring Framework Web Services project which makes it trivially easy to create web services if you already know Java and Spring. But you can look at their code to see how they did what they did.
You can also consider this article that explains using struts2 framework to develop web services.
Apache CXF comes with a slew of examples of web services for a variety (SOAP, REST, etc) of protocols, transports, etc. The examples include inheritance issues that come up in service design (i.e., OO), not to mention CXF itself as a giant example of a Java system.
Go to Axis2 site, they have published very good example (Stock update ) .
This Axis2 is a SOAP engine.
I'm looking for a light version of REST for a Java web application I'm developing.
I've looked at RESTlet (www.restlet.org) and the REST plugin for Struts 2, but I haven't made up my mind. I'm leaning towards RESTlet, as it seems to be lighter.
Has anyone implemented a RESTful layer without any of the the frameworks or with the frameworks?
Any performance issues that you've seen because of the new web layer?
Did the introduction of REST added unmanageable or unreasonable complexity to your project? (Some complexity is understandable, but what I mean is just plain overkilling your design just to add REST)
I'm a huge fan of JAX-RS - I think they've done a great job with that specification. I use it on a number of projects and its been a joy to work with.
JAX-RS lets you create REST resources using POJOs with simple annotations dealing with the URI mappings, HTTP methods and content negotiation all integrated nicely with dependency injection. There's no complex APIs to learn; just the core REST concepts (URIs, headers/response codes and content negotiation) are required. FWIW JAX-RS is quite Rails-ish from the controller point of view
There are a number of JAX-RS implementations out there - see this thread for a discussion.
My personal recommendation is to use Jersey as its got the biggest, most active community behind it, has the best features at the time of writing (WADL support, implicit views, spring integration, nice REST client API); though if you are using JBoss/SEAM you might find RESTeasy integrates a little better.
I'm a big fan of Restlet, but I usually use it to implement apps whose primary role is to be a RESTful web service. It sounds like you're looking to add a RESTful API to an existing application. If that's the case, JAX-RS's (or Enunciate's) annotation-based approach might be a better fit for your project.
As for Restlet, I can tell you that I've been very impressed with the developers and the community; they're very active, engaged, responsive, and committed to a stable, efficient, reliable, and effective framework. My single favorite aspect of the framework is that it is a ground-up implementation of the REST paradigm; therefore there is no impedance-mismatch between a Restlet app's external API and internal implementation. I also really like how flexible it is - it can run inside a Java application container/server such as JBoss, Tomcat, Jetty, etc, or standalone, with an embedded HTTP server library.
Well, I've used Enunciate quite a bit. It uses simple annotations to provide either REST and/or SOAP endpoints.
http://enunciate.codehaus.org
Plus, Ryan Heaton has always provided top-notch support for things, too.
You know there is a new JCP API for Accessing RESTful Services, also:
JAX-RS JCP311
https://jsr311.dev.java.net/
The open source version is called Project Jersey
I am working on a REST API for gliffy.com and we ended up rolling our own. We didn't want to have to bring in Struts 2, Spring, or any other framework. I looked at RESTLet and found it incredibly confusing and over complicated.
Apache has an implementation of the JAX-RS spec, but it is not finalized and also has some oddities to it. We're tentatively planning to open source our solution, but that's not for a few months.
Rolling your own is easy, though. The Servlet Specification gives you everything you need, and you can easily connect to a database via Hibernate (see http://www.naildrivin5.com/daveblog5000/?p=39 for how to set up JPA without using EJB3).
I found restlet to be a really elegant architecture. I'm working in the .net world so it was not an option for me, but I was able to build my own framework following the same basic principles of restlet.
I have found the conversion of our WCF contract-based SOA application to REST based one has significantly simplified the application,