Does anyone have any sample code on how to create a RESTful application that gets information from a database? I would like to implement the conditional-GET like Vinoski talks about, I'm just not sure how to go about it. I have created a SOAP application using CRUD and I'll like to try and compare it to GET, PUT, POST and DELETE
Thanks
Related
In the laravel docs they have explained using resourceful controllers for using REST and using verbs in the methods of the controllers.
I have manually listed all my routes like this:
Route::get('users/{id}/friends', 'UsersController#friends');
I am not using any resource just the method and the uri.
How can I make this restful? Or is it restful already? Then how do I call the methods in a RESTful manner? I am confused
REST is nothing more than a convention on how data should transfer in the web. If you want to be all nitpicky about it check this out: http://en.wikipedia.org/wiki/Representational_state_transfer#Architectural_constraints
As for laravel, when you generate a controller via the CLI like so:
php artisan controller:make UsersController
Laravel automatically creates the boiler plate for the various actions you need to be handling (create, store, destroy, update and so on.) and then you can set-up your routes. Of course you wont need all for most controllers.
Here's a helpful set of links from Phillip Brown's blog:
http://culttt.com/2013/07/01/setting-up-your-first-laravel-4-controller/
http://culttt.com/2013/08/12/building-out-restful-controller-methods-in-laravel-4/
If you want to make it RESTful, the simplest way in laravel is to do Route::resource('user', 'UserController), which automatically gives you the routes shown on the laravel web page.
However, if you are trying to look up the friends RESTfully you would want to make a /friends/ resource, and then register it as Route::resource('friends', 'FriendController'). For more on the relationships/friends side of things I would suggest reading this question from 2011, while not laravel specific, it does answer questions about REST.
We are trying to rebuild an app using Backbone or Angular JS. Main criteria for selecting either of them would be:
The Java script Framework should be able to make calls via SOAP as well as REST.
My question is based on the criteria's which framework should I be choosing? I know angular as well as Backbone are good for REST web service. I am not really sure how well they do when we are trying to make SOAP calls. Not much help online either. Has anyone successfully implemented SOAP calls via Angular or Backbons JS?
Backbone.sync method can be easily overriden to make SOAP calls, but it will fall apart if your SOAP service manages anything else than resources, as REST calls in Backbone are made on behalf of either models or collections of models that support the classic CRUD semantics. As for AngularJS, I haven't used it enought to provide you with an informed answer.
I'm kinda stuck at testing Restful model without server-side.
In Google official tutorial step 11
They show real nicely how to get all the phone lists from a local json files using get method.
It is really nice and very simple, but they are lacking something very basic in this tutorial deleting/adding with delete/post methods.
I'm stuck right now because I cannot find a way to add/delete objects using Restful model without implementing server-side DB, of course not permanently just throughout the application life.
Can someone give an example that's built on AngularJS official tutorial, how to implement a simple add of a phone object?
You want to use Angular's $resource service.
Documentation here
That contains a working example similar to what I think you're asking.
I am trying to create a RESTful web service that accepts JSON arguments and gives out a JSON response.
What I want is to accept HTTP requests made to my URL endpoint.
Something like,
POST /the/endpoint HTTP/1.1
Host: mywebsite.com
{"name":"yourname", "department":"your_department"}
Do a DB read at the backend and give relevant parameters like, say Manager name, salary etc as a JSON object, as the response.
What's the best way to go about it? I was thinking of using Java servlets for this? Is there a better way?
PS - I am just getting started so detailed answers or links to tutorials as to how to implement it would be much appreciated.
Thanks.
Yes you can easily do this with Servlets and some Json Libs for Marshalling /unmarshalling the Json Object to Java Object.
You can make use of Json libs like
Jackson ,
Gson etc
But you must know that REST application doesnt end with just handling the request and response , but it needs to take care of other non-functional requirements like
Authentication
Authorization
Security etc
Building this from a scratch from a Servlet is overkill and waste of time when there are ready made frameworks that these things for you
My favorite is Spring MVC 3.0
Check their project site for more details
Just to show you how easy to set up one in Spring MVC , check this below tutorial
Spring 3 REST Tutorial
Pls rate the post if it helps , Cheers.
If you want to go with Java, I suggest that you take a look at JAX-RS... And since REST is a complex topic, here is a url with tons of informations on it. http://code.google.com/p/implementing-rest/
As a complete beginner, I believe the best way to implement a (nearly) RESTful API without having to read a lot is simply to implement the API just using HTML pages and HTML forms with the back-end processing to handle them.
The rules are:
Use <a> tags to provide links to related resources. (navigation)
Use <form> tags to initiate any kind of processing operation on the server. (actions)
You can then make it properly RESTful by using progressive enhancement to add Javascript AJAX requests that perform PUT, PATCH, and DELETE instead of using POST for those three (of course, keeping POST for creating resources where the client doesn't know the resultant URI).
You can then click around and test the API in a web browser! Tools like Selenium can automate this.
If you need to provide JSON, this can be added after the API has been designed and tested, although libraries exist to process HTML or XHTML responses too, so JSON isn't necessarily required for machine readability.
if you are using php with symfony try:https://github.com/FriendsOfSymfony/FOSRestBundle this lets you create a real REST full servicer very quick.
Vogella made my day very easy when i started Web services with an super example here with eclipse screen shots ..Have a look here.
I have a web app, that also has an iPhone and Android app using the same API. It hasn't yet been made publicly available, so I wanted to look to convert from SOAP to REST.
I was only able to find a few tutorials that go into thorough explanations of how to code a REST web service, and of those I only found the MSDN one useful. The problem is I got really confused when they started using URI data types inside the object.
My question is, if you are converting SOAP to REST, do you have to recreate all the objects to add the URI? Am I not able to just have a REST entry point, then call one of the classes that retrieve the data?
Once the REST service is made it will only be used by my mobile apps, but not the website (since that can directly access the classes), which makes me not want to change the objects to add a URI. Is that a correct assumption to make, or should the web services also be called by the website?
Sorry if these are newbie questions, but I am struggling to get my head around REST, and I haven't had much experience creating the architecture of potentially high user base apps.
If anyone is able to point me to an actual code set, that would be helpful.
Edit: I am using VS2010, coding in C# and .Net 4.
Thanks a lot,
Andy
Im doing a very similar thing right now :). Rest via wcf isnt too hard, you do have to sometimes add your endpoints in the web config and give it the [webget]/[webinvoce] attributes in the refrence.cs of the web refrence when consuming though c# which is annoying.
Here is a code set i used when getting started. There are a few more on code project too.