Link to home
Start Free TrialLog in
Avatar of pzozulka
pzozulka

asked on

JAX-RS REST services

I am looking to build a servlet using Tomcat/Eclipse to do some simple tasks such as user authentication and retrieving some data from MySQL. I was advised to use REST services. I've never worked with REST services before, and looked into a few tutorials, which didn't seem to be to easy.

My question is, do I need to use REST services or can I just create a regular Web Service in Eclipse to encapsulate my regular Java class?
Avatar of mccarl
mccarl
Flag of Australia image

Using REST just for the sake of using REST isn't necessarily a good idea. But whatever you end up doing, it should be selected based on what is actually going to be "calling" your servlet that you are creating. And this, you haven't given any information on.

So to get a bit more specific help, can you tell us how this new servlet of your is gong to be used? ie. Will it be called from other Java code? Or perhaps from Javascript on a browser via AJAX calls? Will it only be used internally, or is it something that would be accessible from anywhere/anyone via the Internet?    That sort of information what is required to make any sort of REST/non-REST decision.
You can take any of the approach for a simple task.

I would suggest to refer below links to identify the usage of both the approach and figure out the suitable one for your requirement.

http://www.infoq.com/articles/rest-soap-when-to-use-each

http://stackoverflow.com/questions/6569134/rest-vs-soap-web-services
Avatar of pzozulka
pzozulka

ASKER

Thanks. This is a school project I'm working on. We haven't decided what the client will look like yet...maybe java applet or maybe JavaScript/AJAX. Either way my group and I are not too familiar with either approach to whichever is easier is the way we are going to go. Needless to say we have a lot of tutorials to go through. Also security is not a concern. In fact it might all be running from the same laptop.
I would say, that since you are undecided on the client side, go with a REST service. It will give you the most flexibility when it comes to implementing the client. I would really only choose a SOAP service if you already had a SOAP based client that it needs to serve.

Now on to how to make getting your head around REST a little easier... the main thing to take on board is that REST isn't a strictly defined protocol, not like SOAP is anyway. Think of it more like a set of recommendations, you aren't forced to do anything. Just pick and implement the recommendations that fit your objective. For instance, REST talks about supporting "multiple representations" of data. But in your case, this is probably overkill since it will most likely be just your own client connecting to it, and therefore ONE representation of the data is fine. REST also talks about all the different HTTP methods (GET, POST, PUT, DELETE, etc) and what they should be used for. But if you web service only ever retrieves data from you DB and returns it, GET is all you would need. Hopefully, you can see the idea that REST doesn't have to as complicated as many tutorials/docs on the subject suggest. Just take the minimum that you need to satisfy your requirements and you are good to go.

Also, I know school projects may restrict what you can use in terms of external libraries, but if using an Eclipse generated Web Service would have been ok, I am sure that using something like the 'Spring' library to help create a REST-like service should also be ok. If you were to do this, your REST web service could be very simple indeed. Check out the below link for how simple a RESTful service might be, and as an example of using Spring to do it.

http://www.mkyong.com/spring-mvc/spring-3-rest-hello-world-example/
SOLUTION
Avatar of Valeri
Valeri
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for all your help guys, I really appreciate it. I'm just a bit confused about SOAP because I never heard of it. I was under the impression that I was comparing REST vs a basic eclipse web service (not SOAP) as displayed in this YouTube video: http://www.youtube.com/watch?v=o2Vjs8ylmFM

From my understanding, I can create a New Dynamic Web Project, then add New Servlet which would provide me with the skeleton code. I can just print HelloWorld to the webpage. Lastly, I create a new WebService. Here's where everything get's cloudy for me. I already have a Servlet...what does a Servlet do on it's own without a Web Service??

The YouTube tutorial next shows how to create a Web Service which I can tell simply wraps whatever Java class(es) you want inside the Web Service, and makes it accessible via an IP address or domain name on port 8080 via a web browser.

Having said all that, if I create a method in the Servlet to talk to the MySQL DB, wouldn't that suffice as a "Service"? Would this approach even work? Is this approach called SOAP?

If this approach works, I would rather not over-complicate this project by introducing and learning REST. (Although I completely agree that it's good to be familiar with something new)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you mccarl for that explanation. It definitely clears things up a bit. It seems like everyone is saying REST is easier, yet I can't find anything as easy as that "SOAP" YouTube video. Do you know if Eclipse has a similar wizard to create a "REST" web service? I was not able to find any tutorials that are out of the box using Eclipse. All the REST tutorials want me to install Jersey or something similar.

Lastly, my final question:

Is the REST service compatible with both, clients that use Java Applet and clinets that use JS/AJAX?

Is the SOAP service compatible with both, clients that use Java Applet and clients that use JS/AJAX?
Do you know if Eclipse has a similar wizard to create a "REST" web service?
Not that I know of, since as I mentioned, REST is more a set of principles than a concrete protocol like SOAP.

As to your final question, there are probably ways to make either type of service work with either Applets or Javascript, but what we are saying is that the REST service will likely be much easier to interface to, from either Applets or Javascript, than the SOAP service.