Σε αυτό το άρθρο θα μάθουμε πως μπορούμε με το Spring να καλέσουμε και να “καταναλώσουμε” ένα RESTful Web Service με τη χρήση του RestTemplate.
Η ανταλαγή μηνυμάτων JSON με χρήση AJAX στο Web είναι ίσως το δυνατότερο και ευκολότερο εργαλείο ενώς προγραμματιστή. Το spring εκεί με τα @ResponseBody και με τη χρήση του Jackson έχει απλοποιήσει εκπληκτικά τα πράγματα στην ανταλαγή μηνυμάτων. Περισσότερες πληροφορίες γι’ αυτό θα βρείτε εδώ Building a RESTful Web Service. Τι γίνεται στην περίπτωση όμως που θέλουμε να καλέσουμε μια RESTful μεθοδο ενώς άλλου WS μέσα απο μια δικιά μας μέθοδο και να κάνουμε map τα αποτελέσματά της? Το Spring το κάνει επίσης πάρα πολύ εύκολο. Ξεκινάμε…
Ας υποθέσουμε οτι θέλουμε να κάνουμε consume τα δεδομένα απο την κλήση του RESTful api του Facebook που σου φέρνουν τα στοιχεία μιας σελίδας, πχ αυτής της ΖΕΝΙΚΑ. Καλώντας σε εναν broswer αυτό http://graph.facebook.com/zenika.hellas θα δούμε το παρακάτω
{ "id": "150542538445211", "about": "The OpenSource software and Java/J2EE experts in Greece members of Zenika Group a technology leader in Europe with JEE and agile methodologies experts from all over the world", "can_post": false, "category": "Professional services", "category_list": [ { "id": "176831012360626", "name": "Professional Services" } ], "checkins": 10, "cover": { "cover_id": 336835279815935, "offset_x": 0, "offset_y": 37, "source": "https://scontent-b.xx.fbcdn.net/hphotos-xap1/t31.0-8/p180x540/10496123_336835279815935_4854648612448760252_o.jpg" }, "description": "Following its highly successful presence in France, where it has been established as a leader in open source technologies and with a client base spanning from banks to manufacturing, Zenika is now expanding its activities in South East Europe with the establishment of its subsidiary,Zenika Hellas.\nInheriting the principles of its parent company, Zenika Hellas focuses on the provision of IT expertise utilizing Open Source technologies and Agile methodologies in the implementation of business solutions in the region.\n\nThe core services and products of the company are defined across three broad, interlocking areas, namely consulting, training and implementation. The combination of those three forms the basis of the firm\u2019s ability to offer bespoke enterprise services to its clients matching their expectations and needs.\n\nOur global team is at you fingertips ready to offer comprehensive services to your enterprise using the open source technologies to:\n\n*increase the productivity\n*reduce the costs\n*make you more adaptable to changes in technology trends\n\nCall us today to discuss with one of our experts and discover the benefits of our services for your company !\n\nOur values\n\nWhen we set up Zenika, we had in mind the kind of company that we would have liked to work in when we were consultants - a community of experts sharing the following values:\n\n*Continuous improvement\n*Technological expertise\n*The spreading of knowledge\n*The 2.0 company: the consultant at the heart of the business\n*Quality after-sales service\n \nKaizen, or continuous improvement, written into our DNA\n\nThe word \"kaizen\" is the combination of two Japanese words \"kai\" and \"zen\" which mean respectively \"change\" and \"good\". Kaizen is one of the 14 principles which are to be found in Lean methodology. By choosing the name \"Zenika\" - an anagram of \"kaizen\" - we firmly wrote this concept of continuous improvement into our genetic code.\n\nAt the cutting edge of technological expertise\nTechnical knowledge has a limited shelf life! With this in mind and still in keeping with the continuous improvement spirit, the Zenika expert is at the centre of three fundamental activities: consulting, development and training. This triple competence makes it possible to better master the multiple facets of a particular technology in order to adapt it to the client's needs.\n\nZenika's commitment to the spreading of knowledge.\n\nConstantly keeping knowledge up to date is one of the pillars of continuous improvement. This explains why Zenika invests in and is totally committed to the spreading of knowledge.\nSince the end of 2008 we've organized in the range of 20 conferences a year, conferences given by internationally-recognized speakers.\n\nOur consultants benefit from time dedicated to R&D, the production of blogs or the creation of user groups.\n\nWe sponsor conferences and industry events.\n\nWe contribute to Open Source projects.\n\nOn the first Wednesday of each month we organize our ZenLabs during which our consultants share in-house their knowledge regarding technology and associated themes.\n \nA 2.0 company model\n\nOpenness is an integral part of our relationship with our consultants. We involve them deeply in the logic of continuous improvement, in the relationship with our clients and in the amount of business that they generate as well as in their career development within the company.\n\nEach one of our consultants enjoys:\n\n*performance-based bonuses\n*professional language training\n*a company iPhone with an unlimited internal call package\n *a true after-sales service\n", "has_added_app": false, "hours": { "mon_1_open": "09:00", "mon_1_close": "18:00", "tue_1_open": "09:00", "tue_1_close": "18:00", "wed_1_open": "09:00", "wed_1_close": "18:00", "thu_1_open": "09:00", "thu_1_close": "18:00", "fri_1_open": "09:00", "fri_1_close": "18:00" }, "is_community_page": false, "is_published": true, "likes": 1539, "link": "https://www.facebook.com/zenika.hellas", "location": { "city": "Her\u00e1kleion", "country": "Greece", "latitude": 38.045727828163, "longitude": 23.767915467187, "street": "Kifisias 7", "zip": "14121" }, "name": "Zenika Hellas", "parking": { "lot": 0, "street": 0, "valet": 0 }, "phone": "+30 2102835550", "talking_about_count": 1, "username": "zenika.hellas", "website": "http://www.zenika.gr https://twitter.com/ZenikaGR http://www.j2ee.gr", "were_here_count": 10 }
Για να μπορεσουμε προγραμματιστικά να καταναλώσουμε το παραπάνω το Spring μας δίνει την template class RestTemplate.
Για να μπορέσουμε να το κάνουμε θέλουμε 2 πράγματα
- Ενα μοντέλο (model) για να αποθηκεύσουμε τα στοιχεία
- Ένα repository για να φέρει τα στοιχεία.
Model
package zenika; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true) public class Page { private String name; private String about; private String phone; private String website; public String getName() { return name; } public String getAbout() { return about; } public String getPhone() { return phone; } public String getWebsite() { return website; } }
Όπως βλέπουμε παραπάνω δεν χρησημοποιούμε όλα τα πεδία του JSON αλλά μόνο αυτά που θέλουμε. Αυτό το πετυχαίνουμε με τη χρήση του @JsonIgnoreProperties
Repositoy
package zenika; import org.springframework.web.client.RestTemplate; @Repository public class FacebookRepository{ public Page getFacebookPage() { RestTemplate restTemplate = new RestTemplate(); Page page = restTemplate.getForObject("http://graph.facebook.com/zenika.hellas", Page.class); return page; } }
To @Repository δεν ειναι απαραίτητο, απλα το βάζω για να μπορέσω μετα στο πρόγραμμά μου να κάνω αυτή την κλάσση autowire.
Επειδή το Jackson JSON processing library είναι στο classpath, το RestTemplate θα το χρησημοποιήσει (μέσω του message converter) για να μετατρέψει τα εισερχόμενα JSON data σε Page object.
Όπως είδαμε, χρησημοποιήσαμε ΜΟΝΟ το RestTemplate και για να κανουμε HTTP GET request αλλά και τη μετρατοπή σε object. To RestTemplate υποστηρίζει όλες τις HTTP μεθόδους όπως POST, PUT, and DELETE.
Enjoy… 🙂
Υ.Γ.
Τα Maven dependencies που χρησημοποιήσαμε είναι
org.springframework.boot spring-boot-starter org.springframework spring-web com.fasterxml.jackson.core jackson-databind
Πολύ καταπληκτικό το άρθρο σας!
Συγχαρητήρια!