REST Basics
REST Basics
REST Basics
{ JSON } http://
SWEN-261
Introduction to Software
Engineering
Department of Software Engineering
Rochester Institute of Technology
What is an API?
▪ APIs (Application Program Interfaces) allow applications to communicate
with one another
• Applications that communicate via APIs can be located on the same computer, over a
local network, or over the internet
▪ An API is a contract between a client application and a service application
• The client application sends a request in an agreed upon format to the API of the
service application
• The service application API sends a response back to the client in an agreed upon
format
• Neither the client application nor the service application need to know the
implementation details of the other
▪ APIs allow access to resources while maintaining security and control
2
APIs in Action
Consider a travel website
Travel Website
• Its “product” is a one-stop shop for a customer’s travel needs
• The travel company itself does not directly provide nor control
Weather Hotel
the travel services
• It must rely on other companies for these services, needs access
to their data, schedules, etc, and does so via APIs Airline Credit
Weather – warn customers of advisories and warnings
Airline – compare fares and schedules, book flights Car Rental
Car Rental – compare rates and availability, reserve cars
Hotel – compare rates and availability, reserve rooms
Credit – payments
Another
Auto Insurance
Now consider the benefits the service provider gains by Travel Website
Name Value
{
"firstName": "Pete", String
"lastName": "Jones"
"year": 2, Number
"address": {
"street": "50 Main St",
"city": "Rochester", Object
"state: "NY",
"zipCode": "14623"
Array
},
"classes": ["SWEN-261", "MATH-181", "ENGL-150"],
"avatar": null,
"enrolled": true Null
}
Boolean
REST HTTP Methods
▪ The most commonly used HTTP methods in REST carry out CRUD operations
(Create, Read, Update, Delete)
• POST – Create a new resource
POST /petstore/pets/dog Create a new dog
• GET – Read access to a resource
GET /petstore/pets Get all pets
GET /petstore/pets/dog/{id} Get a specific dog
• PUT – Update or create a resource
PUT /petstore/pets/dog/{id} Update a specific dog
• DELETE – Delete a resource
DELETE /petstore/pets/dog/{id} Delete a specific dog (because it went to a loving home)
▪ When you type a URL into a browser, an HTTP GET request is sent to the
website and the response data is used to render the page
REST Request Components
▪ Deletes a resource
▪ Request
• The URI specifies the resource to be deleted
http://localhost:8080/jedi/3
• Header
Generally not applicable
• Body
Not Applicable
▪ Response
• Common Status Codes
200 – OK
404 – NOT FOUND
• Header
Application dependent
• Body
Not applicable
Accessing a REST API
17
REST API Frameworks
▪ Nearly every language has REST frameworks available, most are open
source, that support rapid and reliable development
▪ We will use Java and the Spring Boot framework in our term project
• Spring Boot provides the scaffolding for stand-alone, light-weight, production-grade
REST API applications
• Includes an embedded Tomcat server that hosts your APIs and makes them available
to clients on a network
• Routes HTTP requests to your class methods for handling
• Built-in support for serialization and deserialization
• The Spring Initializr wizard, available at start.spring.io or via VSCode extension,
quickly builds a baseline project
You will not need to use Spring Initializr as the starter projects are provided
• Many annotations, e.g. @RestController, are available to easily control
configuration
• See the course resources page for more information and helpful links
18