r/learnjava • • Feb 08 '23

API, REST & RESTfull

what is the difference between API, REST and RESTful?

I was researching articles, blog and what I understood was: API is a ready-made application that can be implemented in your project. REST, an architecture that contains functions: PUT, DELETE, POST, GET, etc. RESTfull, and the application of REST.

But I'm still trying to understand what exactly an API would be? any coding that contains controller, service and model and an API?

Can I create a workshop API and then implement a front end layer on top?

44 Upvotes

17 comments sorted by

•

u/AutoModerator Feb 08 '23

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full - best also formatted as code block

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

35

u/PMmeYourFlipFlops Feb 08 '23
  • API: Service that listens to requests and returns JSON data to be consumed by a frontend.

  • REST is a design pattern where such requests have a distinctive type and function, like PUT, DELETE and the such.

  • RESTFUL means that the service is compliant with the REST specification.

So let's say you have a blog. You have a React frontend and a Spring Boot backend. If you were to create a new blog post, you would send a POST request from react to /posts, which is your RESTful service and it triggers the create method in the Post model using the data you sent it as the payload.

If you want to update your post, you can send a PUT or PATCH request with the new data.

If you want to delete your post, you send a DELTE request.

Look into RESTular and Postman.

3

u/[deleted] Feb 08 '23

I understand now. Thanks!!

2

u/nutrecht Feb 09 '23 edited Feb 09 '23

API: Service that listens to requests and returns JSON data to be consumed by a frontend.

While this is a very common occurance and also probably what OP is talkign about in this context, it's important to note that the actual definition of API is MUCH broader than just services that return JSON.

In addition; you oversimplified. Which IMHO in the context of what OP is asking and the level they are at is totally fine. But also things like that POST is to create resources and PUT is to update them, is only superficially correct.

1

u/[deleted] Feb 08 '23

You got some good source material to learn this topic. You phrased better than most of my professors.

1

u/PMmeYourFlipFlops Feb 08 '23

Thank you. It's only superficially complex at the beginning, but once it clicks it's actually rather simple to understand.

1

u/Angelthree95 Feb 08 '23

API doesn't have to return JSON data. It can be any type ranging from JSON, XML to binary or no data.

1

u/PMmeYourFlipFlops Feb 08 '23

REST only returns JSON.

3

u/nutrecht Feb 09 '23

That really isn't true. While it's the most commonly used serialization format, it could just as well return anything else. Content negotiation is part of HTTP so you could even have the same endpoint return both XML and JSON.

Just an example:

16

u/greglturnquist Feb 08 '23

APIs are simply code you can invoke, be they libraries out on the classpath, remote SOAP XML services, or JSON-based web services.

REST is a collection of constraints on remote services such that you dont have to upgrade your client JUST BECAUSE you updated the service. (Stuff like SOAP is much more brittle)

“RESTful” is a spectrum of trying to describe how many (or how few) of the concepts of REST have really been adopted.

5

u/meioaesmo Feb 08 '23

API means its an interface. When you call a native method from any programming language, like is_null(), you are interacting with code through an interface.

REST is a kind of API, SOAP is another one. APIs are everywhere and are basically an easier way to pass parameters and execute code.

9

u/ipaintfishes Feb 08 '23

An api defines a programming interface, not necessarily producing Json. A good example of a different type would be one using SOAP (xml)

4

u/nutrecht Feb 09 '23

API is actually a very broad term. It stands for Application Programming Interface. So where a Graphical User Interface is a way for people to interact with software, an Application Programming Interface is a way for applications to interact with software, other applications.

So:

API is a ready-made application that can be implemented in your project.

To a large extent, it's just not limited to 'applications'. Any software that is designed to be interacted with through other software, is an API. And even a GUI can be accessed as if it's an API; that's what 'screen scraping' does; software pretending to be a human accessing stuff through a browser.

In the more narrow context of REST API's; these are typically services that other pieces of software interact with over HTTP, and typically use JSON to serialize data. As an example, if we look at your user profile:

REST is a set of guidelines on how to design an API that can be accessed via HTTP, typically (but not limited to) using JSON. It's basic principles are to see data you access as resources. Similar how your profile page is a resource: https://www.reddit.com/user/CupHeap would internally for reddit be a path where CupHeap would be the ID of the 'resource' that is your user profile. So the main design principles for REST are:

To answer your questions:

But I'm still trying to understand what exactly an API would be? any coding that contains controller, service and model and an API?

Assuming the controller returns JSON, then yes. If your controller just serves HTML for users to see, it would generally not considered an API. But again; it's a very broad term.

Can I create a workshop API and then implement a front end layer on top?

Yes :)

Building API's in Spring is my bread and butter and I've been doing that for over a decade. Before REST became fashionable we used SOAP (which REST was a response to, because SOAP was a mess). Before SOAP was fashionable we just created our own formats and used direct TCP/IP connections ;)

1

u/maethor Feb 09 '23

Before SOAP was fashionable we just created our own formats and used direct TCP/IP connections ;)

Don't forget CORBA.

8

u/teefj Feb 08 '23 edited Feb 08 '23

REST != HTTP

The HTTP verbs POST, GET, PUT, PATCH, and DELETE are not defined in REST. Honestly it can be very confusing at first to see what the actual foundations of REST are.

You could read the original paper by Roy Fielding defining REST. It was published 23 years ago. Or you could skim some medium articles and hope the author understands what they are preaching. Different entities might describe how to write a RESTful API that Fielding would disagree with. So try to pick up the core concepts and move on. I think it's easy to start with the idea that the client should be separated from the server, i.e. the user interface is one application that talks to a different application which handles data (server). Then continue on down the rabbit hole.

The vast majority of newer APIs are web based, so using HTTP makes sense. But if you're making a windows application, you may need to target the Windows API, which is not inherently RESTful AFAIK.

-1

u/v4dk4n Feb 08 '23

API is the interface with which you can communicate with a server. In practice this means the endpoints of the application, the controller methods, plus login, logout.

REST is a specification.

The more you comply with this specification, the more your application is RESTful.

-1

u/darrkass Feb 08 '23

It's just exchanging JSONs over HTTP