Operation | REST method | Sample URI |
---|---|---|
Return item(s) | GET | /books (all books) /books/ (all books) /books/12 (one book with ID = 12) |
Create a new item | POST | (you pass the ID in the object itself) /books /books/ |
Update an item | PUT | (you pass the ID in the object itself) /books /books/ |
Delete an item | DELETE | /books/12 (book with ID = 12) |
I will show you some very basic Dojo code so that you can get the idea. Dojo is a client side technology, written in JavaScript, and it doesn't care whether the service behind REST is implemented in Java or any other language.
Let's look at the sample code. Any HTML page can handle this, you don't even need a servlet container.
// I got the idea from an IBM tutorial. function _request(method, xhrArgs) { if (!xhrArgs.url) { var deferred = new dojo.Deferred(); deferred.errback(); return deferred; } xhrArgs.url = xhrArgs.url; return dojo[method](xhrArgs); }
This is a helper function to handle server requests that we will use for GET, PUT, POST and DELETE requests. It uses a Deferred object. Deferred is Dojo's way of handling "threads" and asynchronous matters. This is an important thing to mention: the calls we will make are asynchronous.
Now, let's see the actual code that gets stuff from the server using the function mentioned above. The first code snippet shows a function that gets all the books.
function retrieveBooks() { var deferred = _request("xhrGet", { url: "http://localhost:8080/restsample-0.0.1-SNAPSHOT/rest/books.json", handleAs: "json" }); deferred.addCallback(this, function(value) { // put retrieved picklists in select.options[] var books = value.books; alert(books.length + " books retrieved."); if (books.length > 0) { alert("The first retrieved book is: " + books[0].name); } }); deferred.addErrback(this, function(value) { alert("Error while retrieving books: " + value); }); }
Okay, so that is a simple (and quite useless in real life because it only alerts the results) function that gets data using our previously defined _request function.
xhrGet is a Dojo function. It specifies that we want to call a GET method (and not a PUT etc.).
Next, we specify the address. Note that it ends in .json just in case the browser decides to get XML instead (even though we defined JSON as default).
Then we add callbacks, one for success (addCallback) and one for error handling (addErrback). Look how easily data is read, that's thanks to JSON. No evil eval, no explicit parsing. Why callbacks? Well, that's exactly because the call is asynchronous and we do not precisely know when we are going to get results.
The error callback will handle 500, 404 and other HTTP error codes. That's right. When a server side exception occurs the error callback is called.
Let us also look at a function that retrieves just one book instead of all:
function retrieveBook(bookId) { var deferred = _request("xhrGet", { url: "http://localhost:8080/restsample-0.0.1-SNAPSHOT/rest/books/" + bookId + ".json", handleAs: "json" }); deferred.addCallback(this, function(value) { // put retrieved picklists in select.options[] var book = value.book; alert("The retrieved book is: " + book.name); }); deferred.addErrback(this, function(value) { alert("Error while retrieving book: " + value); }); }
It should now be self explanatory.
Download source code for this article
- REST with Spring 3.0, Spring MVC and Dojo. Part 1 - GET
- REST with Spring 3.0, Spring MVC and Dojo. Part 2 - GET from Dojo perspective
- REST with Spring 3.0, Spring MVC and Dojo. Part 3 - POST and JSR-303 validation
- REST with Spring 3.0, Spring MVC and Dojo. Part 4 - PUT (updating objects)
- REST with Spring 3.0, Spring MVC and Dojo. Part 5 - DELETE
Cattle feed Bags supplier
ReplyDeleteRice Bags Manufacturers
dry fruit pouches manufacturers
Dominican Republic Hong Kong Web Hosting
ReplyDeleteDominican Republic Slovakia Web Hosting
Dominican Republic Bahrain Web Hosting
Dominican Republic Web Hosting India
Dominican Republic Iran Web Hosting
Dominican Republic Moldova Web Hosting
Dominican Republic Turkey Web Hosting
ReplyDeleteEXCELLENT INFORMATION AND THANKING YOU
INDIAN ADVOCATE RESUME FORMAT DOC
BYPASS MAC FILTERING ANDROID
HTML IMAGE ROLLOVER
OP AMP ADDER AND SUBTRACTOR THEORY
THE PROFIT OBTAINED BY SELLING AN ARTICLE FOR RS 480
THE LCM OF THREE DIFFERENT NUMBERS IS 1024
ERROR [ERR_HTTP_HEADERS_SENT]:
CANNOT SET HEADERS AFTER THEY ARE SENT TO THE CLIENT
GIVEN SIGNS SIGNIFY SOMETHING AND ON THAT BASIS AMCAT
ZOHO APTITUDE QUESTIONS 2019 PDF
HOW TO HACK HOTSPOT PASSWORD
ReplyDeleteafghanistan hosting
angola hosting
afghanistan web hosting
bahrain web hosting
belize web hosting
india shared web hosting
italy web hosting
ReplyDeletesuden web hosting
tunisia hosting
uruguay web hosting
inplant training in chennai
nice
ReplyDeletefinal year project dotnet server hacking what is web hosting
macao web hosting
cloud computing virtual server canada hosting
italy hosting
kenya shared web hosting
inplant training in chennai
good...!
ReplyDeletehosting
india hosting
india web hosting
iran web hosting
technology 11 great image sites like imgur hosting
nice information......
ReplyDeleteree internship in bangalore for computer science students
internship for aeronautical engineering
internship for eee students in hyderabad
internship in pune for computer engineering students 2018
kaashiv infotech internship fees
industrial training certificate format for mechanical engineering students
internship report on machine learning with python
internship for biomedical engineering students in chennai
internships in bangalore for cse
internship in coimbatore for ece
very nice post...
ReplyDeleteinternship report on python
free internship in chennai for ece students
free internship for bca
internship for computer science engineering students in india
internships in hyderabad for cse students 2018
electrical companies in hyderabad for internship
internships in chennai for cse students 2019
internships for ece students
inplant training in tcs chennai
internship at chennai
very nice post.........
ReplyDeleter programming training in chennai
internship in bangalore for ece students
inplant training for mechanical engineering students
summer internships in hyderabad for cse students 2019
final year project ideas for information technology
bba internship certificate
internship in bangalore for ece
internship for cse students in hyderabad
summer training for ece students after second year
robotics courses in chennai
Nice Infromation....
ReplyDeleteinternship in chennai for ece students with stipend
internship for mechanical engineering students in chennai
inplant training in chennai
free internship in pune for computer engineering students
internship in chennai for mca
iot internships
internships for cse students in hyderabad
implant training in chennai
internship for aeronautical engineering students in bangalore
inplant training certificate
nice post.......
ReplyDeleteapache solr resume sample
apache spark sample resume
application developer resume samples
application support engineer resume sample
asp dotnet mvc developer resume
asp net core developer resume
asp net developer resume samples
assistant accountant cv sample
assistant accountant resume
assistant accountant resume sample
excellent...!!!
ReplyDeleteSelenium training in chennai
Industrial visit in chennai
Internship
Internships in bangalore for cse students 2019
Free internship in chennai for cse students
Network security projects for cse
Ccna course in chennai
Inplant training in chennai for cse
Inplant training for eee students
Kaashiv infotech chennai
nice....
ReplyDeletecategory/advocate-resume
category/agriculture-forestry-fishing
category/android-developer-resume
category/assistant-professor-resume
category/chartered-accountant-resume
category/database-resume
category/design-engineer-resume
category/developer-resume
category/engineer-resume
category/entrepreneur-and-financial-services-resume
good..nice..
ReplyDeleteassistant-director-resume-format
assistant-director-resume-sample
assistant-professor-resume-sample
back-office-executive-resume-samples
bank-branch-manager-resume-samples
basketball-coach-resume-sample-coach-resumes
bca-fresher-resume-sample
best-general-manager-resume-example
bpo-resume-freshers-sample
bpo-resume-samples-for-freshers
good ....nice...
ReplyDeleteresume/category/software-testing-resume
resume/category/sslc-resume
resume/category/storekeeper-resume
resume/category/stylist-resume
resume/category/teachers-resume
resume/category/technical-architect-resume
resume/category/web-developer-resume
cics-system-programmer-resume-example
resume/cisco-network-engineer-resume
resume/cisco-network-engineer-resume-sample
good.....nice..
ReplyDeletecategory/maintenance-resume
category/manager-resume
category/mechanical-engineering-resume
category/network-engineer-resume
category/officer-resume
category/operations-resume
category/process-associate-resume
category/quality-control-resumes
category/software-engineer-resume
good... nice... very useful..
ReplyDeleteassistant-director-resume-format
director-resume-sample
assistant-professor-resume-sample
back-office-executive-resume-samples
bank-branch-manager-resume-samples
basketball-coach-resume-sample-coach-resumes
bca-fresher-resume-sample
best-general-manager-resume-example
bpo-resume-freshers-sample
bpo-resume-samples-for-freshers
it is best blogs ....
ReplyDeletecivil-engineer-resume-format
client-service-executive-resume-sample
cognos-developer-resume-samples
college-lecturer-resume
college-lecturer-resume-sample
commercial-assistant-resume-sample
compliance-officer-resume-samples
computer-teacher-resume-format
computer-teacher-resume-sample
cordova-developer-resume-sample
excellent...!!!
ReplyDeleteSelenium training in chennai
Industrial visit in chennai
Internship
Internships in bangalore for cse students 2019
Free internship in chennai for cse students
Network security projects for cse
Ccna course in chennai
Inplant training in chennai for cse
Inplant training for eee students
Kaashiv infotech chennai
good .........very useful
ReplyDeletefresher-marketing-resume-sample
front-end-developer-resume-sample
full-stack-developer-resume-samples
fund-accountant-resume-samples
general-ledger-accountant-resume-sample
government-jobs-resume
hadoop-developer-sample-resume
hadoop-developer-sample-resume
hardware-and-networking-resume-samples
hardware-engineer-resume-sample
This comment has been removed by the author.
ReplyDeleteNice blog, very informative content.Thanks for sharing, waiting for next update...
ReplyDeleteVMware Training in Chennai
VMware course in Chennai
VMware Training in Bangalore
VMware course in
Bangalore
R Programming Training in Chennai
ui ux design course in chennai
R Programming Training in Bangalore
Nice blog, very informative content.Thanks for sharing, waiting for the next update...
ReplyDeleteFrench Classes in Chennai
french courses in chennai
french institute in chennai
French Language Classes in Chennai
french classes in bangalore
french language classes in bangalore
TOEFL Coaching in Chennai
Informatica Training in Chennai
valuable blog,Informative content...thanks for sharing, Waiting for the next update...
ReplyDeleteTableau Training in Chennai
Tableau Course in Chennai
Tableau Training in Bangalore
Tableau Certification in Chennai
Spark Training in Chennai
Appium Training in Coimbatore
google 1719
ReplyDeletegoogle 1720
google 1721
google 1722
google 1723
You are sharing excellent information.
ReplyDeleteSMO Company in Chennai
google adwords company in Chennai
google adwords Services company in Chennai
This is really a great information from your post.
ReplyDeleteDevOps Training in Hyderabad
DevOps Course in Hyderabad
In the last couple of years, REST has emerged as a compelling alternative to SOAP/WSDL/WS-*-based distributed architectures. Therefore, I would like to thank you for your efforts in writing this innovation management thesis topics
ReplyDeletearticle. All content has been thoroughly researched. Thank you very much...
I am going to write a few posts on RESTful application development ,apple pencil 2 price in pakistan with Spring 3.0, using Spring MVC, Dojo and Maven.
ReplyDeleteBlockchain technology is revolutionizing the way we think about data and transactions, and the demand for experienced developers in this field is growing rapidly. The Spring MVC framework is a great way to create powerful, secure applications that make use of blockchain technology. With its robust set of features, such as support for multiple languages, easy integration with existing systems, and support for a variety of databases, Spring MVC provides an ideal platform for top blockchain developers to create amazing applications. If you're looking for a great way to get started in the blockchain development world, Spring MVC is definitely worth exploring.
ReplyDeleteI recently started using Insights Artist with Spring 3.0 and Spring MVC, and I'm really impressed with how easy it makes creating REST APIs. The documentation is really well written and the tutorials are extremely helpful. I've been able to quickly set up my API endpoints and handle requests with ease. I'm looking forward to exploring more of Insights Artist's features and capabilities.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI am really appreciated your work to post a valuable blog. I gained a more information about this. I wanna see more posts like a good content. I always prefer to read a good quality content...I found this blog very helpful.
ReplyDeleteAbogado De Divorcio En Virginia