I had a discussion with an employee, he really loves REST, but I still need to make sure of the benefits.
My main problem is that I really do not see REST as an API or an interface in general from the point of view of a consuming application. Let me clarify. We have two applications in which one calls the other using the RESTful API. This is implemented using JAX-RS and RESTeasy. However, using RESTeasy, it is rather difficult to create an interface-based REST client.
So let me say that this is a system for books and authors. The application should know about the book and allow it to be assumed that it already knows some identifier.
- In REST, it calls, for example,
http://server/book/21 , returns an arbitrary payload and deserializes it into a Book object. - Using the RESTeasy client, we have the
BookService interface with the Book getBook(int bookId) , we just call getBook(21) and get the Book object.
What I'm trying to do is that BookService is a well-defined interface where you (as a programmer) can easily see that the argument that it expects is an identifier and it will return a Book object. Using "just REST", we visit some URL and we get returned arbitrary data. There is no clearly defined interface, you do not know how to create a URL without knowing the information of the internal URL from the server, and you need to manually parse the XML (I hope using XSD).
Another thing. I mentioned books and authors.
When using interfaces, you can simply return the BookService returned by the Book and AuthorService returned by Author s. A Book can have an authorId property, and you can get an Author object by calling Author getAuthor(int authorId) .
When using REST, you call the book’s URL and get some information about the authors, including links to the authors. Then you follow the link to get additional information about the authors. But how do you know where exactly to find this link? And the same questions as before arise: how to build a link, how to find out how to analyze the returned data?
And when mixing the two countries, strange things can happen. If I just want to get the Book by id, I can call the BookService (which internally translates the REST call in any way) and get a good Book object. But if I want to get information about the author, I have this String authorLink , which I must execute to get my Author object. But on the contrary, when my starting point is Author and retrieves it using AuthorService , I get links to books that the author wrote in a collection of strings (URLs) pointing to book objects.
So why is REST considered an API? Why should I use REST for correct (Java) interfaces? And how do I mix two?