Designing A Rest Api With Spring Boot

Photo of
Play on YouTube


REST stands for REpresentational State Transfer.

REST-compliant web services allow the requesting systems to access and manipulate textual representations of web resources by using a uniform and predefined set of stateless operations. — Wikipedia contributors. (2021, March 23). Representational state transfer. In Wikipedia, The Free Encyclopedia. Retrieved 12:51, March 26, 2021

Watch this part of the video

Simplifying further, a REST API communicates with clients in terms of resources on which a client could perform operations such as Read or Retrieve, Create, Update and Delete.


REST API Design principles


Following are some of the basic but most overlooked REST API design principles.

Watch this part of the video

1 | Resources are nouns

What is a Resource? A Resource could be anything that we expose to our client through the REST API.

Following are some examples,

customer

product

quote

price

order

comment

These are all nouns, obviously, but it is important to remember that when designing the URLs which we’ll look in to next.

Following are some examples of resource URLs.

example.com/products

stackoverflow.com/questions

what we don’t want to do with urls is to add verbs infront of the resource such as,

example.com/getproducts

stackoverflow.com/listquestions

This is not how REST compliant URLs are built.

2 | Always use plural nouns for resources in the URL

the same examples we saw earlier, products instead of product and questions instead of question

3 | Appropriate use of HTTP methods

A REST compliant web service has a predefined set of stateless operations, and those are the HTTP methods.

HTTP Method Usage in REST API
GET Retrieve a resource
POST Save or Create a resource
PUT Update or Edit a resource
DELETE Delete a resource

4 | Appropriate Use of HTTP status codes

You can find all the HTTP status codes and what they mean on the following page. https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

Most commonly used HTTP Status codes

HTTP Status code Meaning
200 OK
201 Created
204 No content
400 Bad request
401 Unauthorised
404 Not found

200 codes mean that the request was processed without any issues.

400 codes mean some client error.

5 | HATEOAS

HATEOAS stands for Hypermedia As The Engin Of Application

Simply means to use links for navigating the API.


How To Design A REST API


Now let’s design a REST API applying the basic principles.

Watch this part of the video

Comments API design

If the resource was independent, the URLs would look like the following,

HTTP Method URL Description
GET /comments Return a list of all comments
GET /comments/{id} Return a single comment of which the id is {id}
POST /comments Create or save a new comment
PUT /comments/{id} Update a single comment of which the id is {id}
DELETE /comments/{id} Delete a single comment of which the id is {id}

But in this case, ‘comment’ is more of a sub-resource, because a comment belongs to a post.

You can handle this in different ways but it is a matter of personal preference, or if you are working in a team, you could discuss and decide which approach to use.

So, following is how I designed the URLs.

HTTP Method URL Description
GET /posts/{post-id}/comments Return a list of all comments
POST /posts/{post-id}/comments Create or save a new comment
DELETE /comments/{id} Delete a single comment of which the id is {id}

I’m not allowing comment editing so PUT method will not be used.


Spring Boot Application architecture


The application code is organised into three layers.

Watch this part of the video

Spring Boot Application architecture
Repository This is for exchanging data with the database, i.e. it consists of all the repository interfaces which the service classes would use to access the database.
Service Holds the business logic
Controller holds the API classes which would serve the incoming requests

Checkout -> PUT vs PATCH Operations

PUT vs PATCH

4617作文网周公解梦 梦见结婚周易占卦周易六十四卦金钱卦关于工程公司起名大全自驾游起个好听的名字七月生的鼠宝宝起什么名计算器的快捷命令给今年出世的女孩子起什么名好周公解梦梦见自己掉头发周易八卦象算命哪里周公解梦梦见别人吐血宸字起名字的寓意周易起名名字测带志字装饰公司起名阮姓女孩子起名公司起名免费软件的姓孟的怎么起名字起什么名字好姓李测试生成八字算命无锡市周易数术学会周易预测是什么周易国学服务起名要参考生辰八字好用来起名字周易看八字周公解梦女人梦见下雪周易婚配表大全配起名字测名大师算命很准的大师淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻让美丽中国“从细节出发”清明节放假3天调休1天男子给前妻转账 现任妻子起诉要回网友建议重庆地铁不准乘客携带菜筐月嫂回应掌掴婴儿是在赶虫子重庆警方辟谣“男子杀人焚尸”国产伟哥去年销售近13亿新的一天从800个哈欠开始男孩疑遭霸凌 家长讨说法被踢出群高中生被打伤下体休学 邯郸通报男子持台球杆殴打2名女店员被抓19岁小伙救下5人后溺亡 多方发声单亲妈妈陷入热恋 14岁儿子报警两大学生合买彩票中奖一人不认账德国打算提及普京时仅用姓名山西省委原副书记商黎光被逮捕武汉大学樱花即将进入盛花期今日春分张家界的山上“长”满了韩国人?特朗普谈“凯特王妃P图照”王树国3次鞠躬告别西交大师生白宫:哈马斯三号人物被杀代拍被何赛飞拿着魔杖追着打315晚会后胖东来又人满为患了房客欠租失踪 房东直发愁倪萍分享减重40斤方法“重生之我在北大当嫡校长”槽头肉企业被曝光前生意红火手机成瘾是影响睡眠质量重要因素考生莫言也上北大硕士复试名单了妈妈回应孩子在校撞护栏坠楼网友洛杉矶偶遇贾玲呼北高速交通事故已致14人死亡西双版纳热带植物园回应蜉蝣大爆发男孩8年未见母亲被告知被遗忘张立群任西安交通大学校长恒大被罚41.75亿到底怎么缴沈阳一轿车冲入人行道致3死2伤奥运男篮美国塞尔维亚同组周杰伦一审败诉网易国标起草人:淀粉肠是低配版火腿肠外国人感慨凌晨的中国很安全男子被流浪猫绊倒 投喂者赔24万杨倩无缘巴黎奥运男子被猫抓伤后确诊“猫抓病”春分“立蛋”成功率更高?记者:伊万改变了国足氛围奥巴马现身唐宁街 黑色着装引猜测

4617作文网 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化