Spring boot读书笔记一REST API
REST API Definition
REST = Representational State Transfer
A REST API is a web service that uses HTTP methods to perform operations on resources, following specific architectural principles.
Core Concepts
Resource-Based: Everything is treated as a resource with a unique URL
/users/123 (a specific user)
/products (collection of products)
/orders/456 (a specific order)
HTTP Methods:
- GET - Retrieve data
- POST - Create new resource
- PUT - Update entire resource
- PATCH - Update part of resource
- DELETE - Remove resource
Simple REST API Example
@RestController
@RequestMapping("/api/config")
public class ConfigRestController {
@Autowired
private AppProperties appProperties;
// GET /api/config - Retrieve configuration
@GetMapping
public AppProperties getConfig() {
return appProperties;
}
// GET /api/config/security - Get security settings
@GetMapping("/security")
public AppProperties.Security getSecurity() {
return appProperties.getSecurity();
}
}
HTTP Request/Response Flow
Request:
- GET /api/config HTTP/1.1
- Host: localhost:8080
- Accept: application/json
Response:
{
"name": "MyApp",
"ip": "192.168.1.1",
"port": 8080,
"security": {
"enabled": true,
"token": "abc123",
"roles": ["admin", "user"]
}
}
Key Characteristics
- Stateless: Each request contains all needed information
- Cacheable: Responses can be cached for performance
- Uniform Interface: Consistent way to interact with resources
- JSON/XML: Data exchanged in standard formats
Real-World Usage
Mobile Apps: Fetch user data, submit forms
Web Applications: AJAX calls to load/update data
Microservices: Services communicate via REST APIs
Third-party Integration: Connect with external systems
The AppProperties class would be perfect for a REST API that manages application configuration - clients could retrieve or update configuration settings through HTTP requests.

浙公网安备 33010602011771号