Objective
Understand RESTful web services and their implementation using Spring Boot. Learn to create, test, and consume REST APIs, including JSON-based CRUD operations, using Spring Boot’s REST support, Postman for testing, and RestTemplate
for client-side invocation.
Lecture Topics
1. Introduction to Web Services
- Definition: Web services are standardized ways for applications to communicate over the internet, typically using HTTP.
- Types:
- SOAP: XML-based, protocol-driven, with strict standards (e.g., WSDL).
- REST: Representational State Transfer, lightweight, uses HTTP methods (GET, POST, PUT, DELETE) and typically JSON for data exchange.
- Key Characteristics of REST:
- Stateless: Each request contains all necessary information; no server-side session.
- Resource-Based: Operates on resources identified by URIs (e.g.,
/employees
).
- HTTP Methods: GET (read), POST (create), PUT (update), DELETE (delete).
- JSON/XML: Commonly uses JSON for data representation.
- Benefits:
- Scalability, flexibility, and interoperability.
- Easy integration with web and mobile applications.
2. SOAP vs. RESTful Web Services
- SOAP:
- Uses XML for messaging.
- Requires WSDL for service description.
- Strict standards, complex to implement.
- Suitable for enterprise applications with high security needs (e.g., banking).
- REST:
- Uses JSON or XML, simpler and lighter.
- Leverages standard HTTP methods and status codes.
- Easier to develop, test, and consume.
- Ideal for web and mobile apps, microservices.
- Comparison:
- SOAP: Heavyweight, rigid, secure (WS-Security).
- REST: Lightweight, flexible, widely adopted for APIs.
- Exam Tip: Be able to compare SOAP and REST with examples of use cases.
3. RESTful Web Service Introduction
- Principles of REST (Roy Fielding’s constraints):
- Client-Server: Separation of UI and data storage.
- Stateless: No client state stored on the server.
- Cacheable: Responses can be cached to improve performance.
- Layered System: Clients cannot tell if they’re connected to the end server.
- Uniform Interface: Standard methods (GET, POST, PUT, DELETE), resource URIs, and representations (JSON/XML).
- HTTP Status Codes:
200 OK
: Successful request.
201 Created
: Resource created (e.g., after POST).
204 No Content
: Successful but no content returned (e.g., after DELETE).
400 Bad Request
: Invalid request data.
404 Not Found
: Resource not found.
500 Internal Server Error
: Server-side error.
- REST in Spring:
- Uses Spring MVC annotations (
@RestController
, @GetMapping
, etc.).
- Automatically converts Java objects to JSON using Jackson (included in
spring-boot-starter-web
).
4. Create RESTful Web Service in Java Using Spring Boot
- Key Annotations:
@RestController
: Marks a class as a controller that returns JSON/XML (combines @Controller
and @ResponseBody
).
@RequestMapping
: Maps HTTP requests to methods.
@GetMapping
, @PostMapping
, @PutMapping
, @DeleteMapping
: Maps specific HTTP methods.
@PathVariable
: Extracts variables from the URI.
@RequestBody
: Binds JSON payload to a Java object.