Is the REST API or REST vs Java Interface?

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?

+4
source share
7 answers

Check out the Hypermedia API on the search engine of your choice.

There is some good literature that will explain how you “know” what to call, with what request. In particular, HATEOS .

Why are Hypermedia APIs? REST is a fairly free and irrigated term. Often performed incorrectly. There is a recent upsurge to try to eliminate this confusion, hence the “new” terminology. When everything is done correctly, you will get access to the REST service (see the Hypermedia API Service) using a pleasant style interface that is familiar to you using similar strongly typed services (ala SOAP, RPC) in Java / .NET.

+2
source

No one uses REST in the way Roy Fielding suggested, for some reason. So this is impractical. For lazy people, this is enough not to think about it.

Apparently, the industry is obsessed with inventing different names for RPC.

+2
source

REST is not an API, but more of an architecture. REST is used to communicate between two different systems over existing HTTP protocols. REST makes sense for many things, maybe in your case you don't need to use it.

0
source

In short, the REST API is flexible for other programming languages.

I am not familiar with RESTEasy, but I am familiar with RESTful services. There is no standard for REST. Most REST services publish how to interact with their REST API. What URLs (resources) are available, what type of content to send, and the type to be returned. Good ones will also post which http status codes will be returned and how to handle errors. In your case, I wonder what the REST Easy client does. Is it just converting an API call to an HTTP request and handling everything for the developer?

We document our REST API calls and provide a jar file with all the models. Models use JAXB annotations, so developers do not need to know everything about XML or JSON that is returned from the service. That is, if they use Java and our models. The best part about publishing and documenting the API is that developers of other languages ​​can use the service if they can make HTTP requests. This allows developers to develop client applications in almost any programming language. (Lately, there seem to be more C # implementations).

In addition to providing the model jar file, we provide XSD in our documentation for non-Java developers.

0
source

if you are thinking about REST (architectural design or coding style) about a tool for building distributed applications / systems, and not about using a well-known and proven concept (on the World Wide Web) to control the state of an application and use it in some situations.

when you want to transfer some processing / calculation from one application to any remote host (say, get a list of books), you can do this by serializing your method call (GetBooks) into a SOAP message, and then add this message to the http request and etc. or you can just call GET / books. In some situations, it is much cheaper to use ti in the second way. if I mentioned some other things, such as resource caching, which are part of the infrastructure, and not implemented explicitly or flexibly, when you need different representations of the same resource, then it even makes sense.

if any service uses the REST style and is carefully written (like any API), it is easy to understand and consume. Also, these types of services can be easily used for clients of various types (javascript, php, ..), where there are no frameworks like JAX-WS or WCF.

To simplify, you can think of the REST service as a bookshelf, where you can get several books (resources), where you can post new books or post them that you have. If each resource makes sense in terms of the domain problem, and if the resource view contains links to related resources, then I don’t see the problem of understanding / consumption.

0
source

You misunderstand the important part of REST. In a well-designed RESTful system, two things should be well documented:

  • The entry point (or "cool" URIs) that you use to start interacting with the system.
  • Schema for payload data sent in requests and returned in responses (in HTTP terms, these are media types)

Give me only these two things and I can create a client for your RESTful API. Starting from # 1 and using knowledge from # 2 to find a way to “hyperlink” the system API, I can find the resources I need.

Knowing the presentation of your Book system will allow me to make a GET call to extract one and understand it, or make a POST or PUT request to create it. HTTP supports these verbs out of the box, I just need to know what is sent over the wire (and # 2 tells me about it).

If I don't like XML, I can try asking the server if I can speak JSON instead, and HTTP supports this content negotiation out of the box.

REST is not a magical fairy made by unicorns, and it will not solve your problems simply through use. However, the common sense architecture is trying to use existing well-established, well-understood, scalable and flexible technologies and approaches.

0
source

ReST is an architectural style, not an API.

Some benefits you get with ReST

  • Possibility of multiple response (for example: JSON, XML). REST is not XML bound like SOAP
  • Using JSON makes your payload weight and your service faster
  • It’s much easier to develop and test.
  • ReST uses simple HTTP, so no problems with the proxy server,

Regarding the service definition, you can create the wadl file generated for your ReST service to view the full parameters. Most servers automatically generate them. Services should be well documented, whether SOAP or ReST. You can check out the LinkedIn ReST API .

The only advantage I see for SOAP is the security features that SOAP can provide. But not all applications need this level of security.

0
source

Source: https://habr.com/ru/post/1434714/


All Articles