SlideShare a Scribd company logo
1 of 11
Download to read offline
Building RESTful APIs
best practices by Ania Pietras
What is REST?
REST stands for Representational State Transfer. (It is sometimes spelled
"ReST".) It relies on a stateless, client-server, cacheable communications
protocol -- and in virtually all cases, the HTTP protocol is used.
SOAP REST
More about REST: http://www.restapitutorial.com/lessons/whatisrest.html
REST
URL
HTTP METHOD
HEADER
BODY
USE URL + HTTP METHOD TO
DESCRIBE YOUR API
URL examples
Some GET methods:
www.coolstuff.com/shop - show whole shop
www.coolstuff.com/shop/awesomeThings - show products
from category awesomeThings
www.coolstuff.com/shop/awesomeThings/123 - show details
about awesome things with 123 as ID
www.coolstuff.com/shop/cart - show your cart
www.coolstuff.com/shop/cart/1 -show the first item of your cart
POST method:
www.coolstuff.com/shop/cart + JSON - add awesomeThing
with ID 123 to your cart
DELETE method:
www.coolstuff.com/shop/cart/1 - delete the first item of your
cart
General Rules
for entire collection:
/awesomeThings
for given item:
/awesomeThings/{id}
we control actions by using specific http method
POST Creates a new resource. 201 (Created), 'Location' header
with link to /customers/{id}
containing new ID.
404 (Not Found), 409 (Conflict) if
resource already exists..
PUT Updates an existing
resource.
404 (Not Found), unless you want
to update/replace every resource
in the entire collection.
200 (OK) or 204 (No Content).
404 (Not Found), if ID not found or
invalid
DELETE Deletes a resource. 404 (Not Found), unless you want
to delete the whole collection—not
often desirable.
200 (OK). 404 (Not Found), if ID
not found or invalid.
EDIT Retrieves a resource. 200 (OK), list of customers. Use
pagination, sorting and filtering to
navigate big lists.
200 (OK), single customer. 404
(Not Found), if ID not found or
invalid.
HTTP methods
Entire Collection (e.g. /customers) Specific Item (e.g. /customers/{id})
RESTful Controller in Spring application
@RestController
public class GreetingController {
...
@RequestMapping(value="/hello", method=RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public boolean sayHello() {
return helloService.sayHello()
}
@RequestMapping(value="/hello", method=RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public boolean sayPersonalizedHello(@RequestParam("name") String name) {
return helloService.sayPersonalizedHello(name)
}
}
@RequestMapping(value="/hello/{nickId}", method=RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public boolean sayBroHello(@PathVariable(“nickId”) Long nickId) {
return helloService.sayBroHello(nickId)
}
}
use when you pass id
@RestController = @Controller + @ResponseBody
use when you pass something more complex
than id
Documenting REST API with Swagger
/**
* REST endpoint
*/
@Api(value = "users", description = "Endpoint for user management")
@Path("/users")
public class UsersEndpoint {
...
}
Documenting REST API with Swagger
@GET
@Path("/{userName}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Returns user details", notes = "Returns a complete list of users details with date of last modification.",
response = User.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successful retrieval of user detail", response = User.class),
@ApiResponse(code = 404, message = "User does not exist"),
@ApiResponse(code = 500, message = "Internal server error")}
)
public Response getUser(@ApiParam(name = "userName", value = "Alphanumeric login to the application", required = true) @PathParam
("userName") String userName) {
...
}
Read more:
http://www.restapitutorial.com/lessons/whatisrest.html
https://codeplanet.io/principles-good-restful-api-design/
http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api

More Related Content

What's hot

Devoxx 2012 hibernate envers
Devoxx 2012   hibernate enversDevoxx 2012   hibernate envers
Devoxx 2012 hibernate enversRomain Linsolas
 
Protect from forgery
Protect from forgeryProtect from forgery
Protect from forgeryChad Harmon
 
Real World Dependency Injection - IPC11 Spring Edition
Real World Dependency Injection - IPC11 Spring EditionReal World Dependency Injection - IPC11 Spring Edition
Real World Dependency Injection - IPC11 Spring EditionStephan Hochdörfer
 
Exceptional Handling in Java
Exceptional Handling in JavaExceptional Handling in Java
Exceptional Handling in JavaQaziUmarF786
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to ProductionMark Baker
 
Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVARajan Shah
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in JavaPrasad Sawant
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Technical training sample
Technical training sampleTechnical training sample
Technical training sampleopenerpwiki
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling ConceptsVicter Paul
 
Compatibility Detector Tool of Chrome extensions
Compatibility Detector Tool of Chrome extensionsCompatibility Detector Tool of Chrome extensions
Compatibility Detector Tool of Chrome extensionsKai Cui
 

What's hot (20)

Devoxx 2012 hibernate envers
Devoxx 2012   hibernate enversDevoxx 2012   hibernate envers
Devoxx 2012 hibernate envers
 
Java exception
Java exception Java exception
Java exception
 
Exception handling
Exception handlingException handling
Exception handling
 
Protect from forgery
Protect from forgeryProtect from forgery
Protect from forgery
 
Maze
MazeMaze
Maze
 
Real World Dependency Injection - IPC11 Spring Edition
Real World Dependency Injection - IPC11 Spring EditionReal World Dependency Injection - IPC11 Spring Edition
Real World Dependency Injection - IPC11 Spring Edition
 
Exceptional Handling in Java
Exceptional Handling in JavaExceptional Handling in Java
Exceptional Handling in Java
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to Production
 
Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVA
 
Exception
ExceptionException
Exception
 
Built in exceptions
Built in exceptions Built in exceptions
Built in exceptions
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
70562 (1)
70562 (1)70562 (1)
70562 (1)
 
Technical training sample
Technical training sampleTechnical training sample
Technical training sample
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
 
Rails and security
Rails and securityRails and security
Rails and security
 
Compatibility Detector Tool of Chrome extensions
Compatibility Detector Tool of Chrome extensionsCompatibility Detector Tool of Chrome extensions
Compatibility Detector Tool of Chrome extensions
 
Exception handling
Exception handlingException handling
Exception handling
 
Clean tests good tests
Clean tests   good testsClean tests   good tests
Clean tests good tests
 

Viewers also liked

Dlaczego (i jak) się uczyć
Dlaczego (i jak) się uczyćDlaczego (i jak) się uczyć
Dlaczego (i jak) się uczyćAnna Pietras
 
Nowości w Javie 8 okiem programisty
Nowości w Javie 8 okiem programistyNowości w Javie 8 okiem programisty
Nowości w Javie 8 okiem programistyMarcinStachniuk
 
Alternatywne podejście do baz danych - MongoDb
Alternatywne podejście do baz danych - MongoDbAlternatywne podejście do baz danych - MongoDb
Alternatywne podejście do baz danych - MongoDbWojciech Soczyński
 
Mapping in Java Options
Mapping in Java OptionsMapping in Java Options
Mapping in Java OptionsAnna Pietras
 
Co nowego w Javie piszczy – Java 8
Co nowego w Javie piszczy – Java 8Co nowego w Javie piszczy – Java 8
Co nowego w Javie piszczy – Java 83camp
 
Start stop-continue
Start stop-continueStart stop-continue
Start stop-continueAnna Pietras
 
JavaStart - kurs Java Podstawy
JavaStart - kurs Java PodstawyJavaStart - kurs Java Podstawy
JavaStart - kurs Java PodstawyJavaStart
 
Jak napisać CV, które zapewni Ci pracę? 9 wskazówek
Jak napisać CV, które zapewni Ci pracę? 9 wskazówekJak napisać CV, które zapewni Ci pracę? 9 wskazówek
Jak napisać CV, które zapewni Ci pracę? 9 wskazówekPiotr Sosnowski
 

Viewers also liked (8)

Dlaczego (i jak) się uczyć
Dlaczego (i jak) się uczyćDlaczego (i jak) się uczyć
Dlaczego (i jak) się uczyć
 
Nowości w Javie 8 okiem programisty
Nowości w Javie 8 okiem programistyNowości w Javie 8 okiem programisty
Nowości w Javie 8 okiem programisty
 
Alternatywne podejście do baz danych - MongoDb
Alternatywne podejście do baz danych - MongoDbAlternatywne podejście do baz danych - MongoDb
Alternatywne podejście do baz danych - MongoDb
 
Mapping in Java Options
Mapping in Java OptionsMapping in Java Options
Mapping in Java Options
 
Co nowego w Javie piszczy – Java 8
Co nowego w Javie piszczy – Java 8Co nowego w Javie piszczy – Java 8
Co nowego w Javie piszczy – Java 8
 
Start stop-continue
Start stop-continueStart stop-continue
Start stop-continue
 
JavaStart - kurs Java Podstawy
JavaStart - kurs Java PodstawyJavaStart - kurs Java Podstawy
JavaStart - kurs Java Podstawy
 
Jak napisać CV, które zapewni Ci pracę? 9 wskazówek
Jak napisać CV, które zapewni Ci pracę? 9 wskazówekJak napisać CV, które zapewni Ci pracę? 9 wskazówek
Jak napisać CV, które zapewni Ci pracę? 9 wskazówek
 

Similar to Building RESTful APIs: Best Practices for Designing & Documenting RESTful Web Services

RESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSRESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSCarol McDonald
 
Securing RESTful Payment APIs Using OAuth 2
Securing RESTful Payment APIs Using OAuth 2Securing RESTful Payment APIs Using OAuth 2
Securing RESTful Payment APIs Using OAuth 2Jonathan LeBlanc
 
RESTful SOA - 中科院暑期讲座
RESTful SOA - 中科院暑期讲座RESTful SOA - 中科院暑期讲座
RESTful SOA - 中科院暑期讲座Li Yi
 
Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Mario Cardinal
 
Rest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsRest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsCarol McDonald
 
Building RESTful applications using Spring MVC
Building RESTful applications using Spring MVCBuilding RESTful applications using Spring MVC
Building RESTful applications using Spring MVCIndicThreads
 
Developing eCommerce Apps with the Shopify API
Developing eCommerce Apps with the Shopify APIDeveloping eCommerce Apps with the Shopify API
Developing eCommerce Apps with the Shopify APIJosh Brown
 
Designing CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIsDesigning CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIsNeil Crookes
 
Ellerslie User Group - ReST Presentation
Ellerslie User Group - ReST PresentationEllerslie User Group - ReST Presentation
Ellerslie User Group - ReST PresentationAlex Henderson
 
RESTful API 제대로 만들기
RESTful API 제대로 만들기RESTful API 제대로 만들기
RESTful API 제대로 만들기Juwon Kim
 
RestFull Webservices with JAX-RS
RestFull Webservices with JAX-RSRestFull Webservices with JAX-RS
RestFull Webservices with JAX-RSNeil Ghosh
 
Domain Specific Languages (EclipseCon 2012)
Domain Specific Languages (EclipseCon 2012)Domain Specific Languages (EclipseCon 2012)
Domain Specific Languages (EclipseCon 2012)Sven Efftinge
 
01. http basics v27
01. http basics v2701. http basics v27
01. http basics v27Eoin Keary
 
RESTful services
RESTful servicesRESTful services
RESTful servicesgouthamrv
 

Similar to Building RESTful APIs: Best Practices for Designing & Documenting RESTful Web Services (20)

RESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSRESTful Web Services with JAX-RS
RESTful Web Services with JAX-RS
 
Securing RESTful Payment APIs Using OAuth 2
Securing RESTful Payment APIs Using OAuth 2Securing RESTful Payment APIs Using OAuth 2
Securing RESTful Payment APIs Using OAuth 2
 
Doing REST Right
Doing REST RightDoing REST Right
Doing REST Right
 
RESTful SOA - 中科院暑期讲座
RESTful SOA - 中科院暑期讲座RESTful SOA - 中科院暑期讲座
RESTful SOA - 中科院暑期讲座
 
Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.
 
Ws rest
Ws restWs rest
Ws rest
 
Rest
RestRest
Rest
 
Rest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsRest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.js
 
Building RESTful applications using Spring MVC
Building RESTful applications using Spring MVCBuilding RESTful applications using Spring MVC
Building RESTful applications using Spring MVC
 
Web Scraping with PHP
Web Scraping with PHPWeb Scraping with PHP
Web Scraping with PHP
 
PHP-Part4
PHP-Part4PHP-Part4
PHP-Part4
 
Developing eCommerce Apps with the Shopify API
Developing eCommerce Apps with the Shopify APIDeveloping eCommerce Apps with the Shopify API
Developing eCommerce Apps with the Shopify API
 
Designing CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIsDesigning CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIs
 
Ellerslie User Group - ReST Presentation
Ellerslie User Group - ReST PresentationEllerslie User Group - ReST Presentation
Ellerslie User Group - ReST Presentation
 
Standards of rest api
Standards of rest apiStandards of rest api
Standards of rest api
 
RESTful API 제대로 만들기
RESTful API 제대로 만들기RESTful API 제대로 만들기
RESTful API 제대로 만들기
 
RestFull Webservices with JAX-RS
RestFull Webservices with JAX-RSRestFull Webservices with JAX-RS
RestFull Webservices with JAX-RS
 
Domain Specific Languages (EclipseCon 2012)
Domain Specific Languages (EclipseCon 2012)Domain Specific Languages (EclipseCon 2012)
Domain Specific Languages (EclipseCon 2012)
 
01. http basics v27
01. http basics v2701. http basics v27
01. http basics v27
 
RESTful services
RESTful servicesRESTful services
RESTful services
 

More from Anna Pietras

Fluent API for selenium - options
Fluent API for selenium  - optionsFluent API for selenium  - options
Fluent API for selenium - optionsAnna Pietras
 
Global competetiveness of airlines
Global competetiveness of airlinesGlobal competetiveness of airlines
Global competetiveness of airlinesAnna Pietras
 
T mobile international cooperation
T mobile international cooperationT mobile international cooperation
T mobile international cooperationAnna Pietras
 

More from Anna Pietras (6)

Fluent API for selenium - options
Fluent API for selenium  - optionsFluent API for selenium  - options
Fluent API for selenium - options
 
Feedback
FeedbackFeedback
Feedback
 
Be a rockstar!
Be a rockstar!Be a rockstar!
Be a rockstar!
 
Global competetiveness of airlines
Global competetiveness of airlinesGlobal competetiveness of airlines
Global competetiveness of airlines
 
T mobile international cooperation
T mobile international cooperationT mobile international cooperation
T mobile international cooperation
 
My stories
My storiesMy stories
My stories
 

Recently uploaded

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 

Recently uploaded (20)

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 

Building RESTful APIs: Best Practices for Designing & Documenting RESTful Web Services

  • 1. Building RESTful APIs best practices by Ania Pietras
  • 2. What is REST? REST stands for Representational State Transfer. (It is sometimes spelled "ReST".) It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used. SOAP REST More about REST: http://www.restapitutorial.com/lessons/whatisrest.html
  • 4. USE URL + HTTP METHOD TO DESCRIBE YOUR API
  • 5. URL examples Some GET methods: www.coolstuff.com/shop - show whole shop www.coolstuff.com/shop/awesomeThings - show products from category awesomeThings www.coolstuff.com/shop/awesomeThings/123 - show details about awesome things with 123 as ID www.coolstuff.com/shop/cart - show your cart www.coolstuff.com/shop/cart/1 -show the first item of your cart POST method: www.coolstuff.com/shop/cart + JSON - add awesomeThing with ID 123 to your cart DELETE method: www.coolstuff.com/shop/cart/1 - delete the first item of your cart
  • 6. General Rules for entire collection: /awesomeThings for given item: /awesomeThings/{id} we control actions by using specific http method
  • 7. POST Creates a new resource. 201 (Created), 'Location' header with link to /customers/{id} containing new ID. 404 (Not Found), 409 (Conflict) if resource already exists.. PUT Updates an existing resource. 404 (Not Found), unless you want to update/replace every resource in the entire collection. 200 (OK) or 204 (No Content). 404 (Not Found), if ID not found or invalid DELETE Deletes a resource. 404 (Not Found), unless you want to delete the whole collection—not often desirable. 200 (OK). 404 (Not Found), if ID not found or invalid. EDIT Retrieves a resource. 200 (OK), list of customers. Use pagination, sorting and filtering to navigate big lists. 200 (OK), single customer. 404 (Not Found), if ID not found or invalid. HTTP methods Entire Collection (e.g. /customers) Specific Item (e.g. /customers/{id})
  • 8. RESTful Controller in Spring application @RestController public class GreetingController { ... @RequestMapping(value="/hello", method=RequestMethod.GET) @ResponseStatus(HttpStatus.OK) public boolean sayHello() { return helloService.sayHello() } @RequestMapping(value="/hello", method=RequestMethod.GET) @ResponseStatus(HttpStatus.OK) public boolean sayPersonalizedHello(@RequestParam("name") String name) { return helloService.sayPersonalizedHello(name) } } @RequestMapping(value="/hello/{nickId}", method=RequestMethod.GET) @ResponseStatus(HttpStatus.OK) public boolean sayBroHello(@PathVariable(“nickId”) Long nickId) { return helloService.sayBroHello(nickId) } } use when you pass id @RestController = @Controller + @ResponseBody use when you pass something more complex than id
  • 9. Documenting REST API with Swagger /** * REST endpoint */ @Api(value = "users", description = "Endpoint for user management") @Path("/users") public class UsersEndpoint { ... }
  • 10. Documenting REST API with Swagger @GET @Path("/{userName}") @Produces(MediaType.APPLICATION_JSON) @ApiOperation(value = "Returns user details", notes = "Returns a complete list of users details with date of last modification.", response = User.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "Successful retrieval of user detail", response = User.class), @ApiResponse(code = 404, message = "User does not exist"), @ApiResponse(code = 500, message = "Internal server error")} ) public Response getUser(@ApiParam(name = "userName", value = "Alphanumeric login to the application", required = true) @PathParam ("userName") String userName) { ... }