Spring Boot Notes
Spring Boot makes it easy to create stand-alone, production-grade Spring-based applications.
Quick Start
bash
# Create a new Spring Boot project
curl https://start.spring.io/starter.zip -d type=maven-project \
-d language=java -d bootVersion=3.4.0 \
-d groupId=com.example -d artifactId=demo \
-o demo.zipProject Structure
md
src/
├── main/
│ ├── java/com/example/demo/
│ │ ├── DemoApplication.java
│ │ ├── controller/
│ │ └── service/
│ └── resources/
│ └── application.yml
└── test/Common Annotations
| Annotation | Purpose |
|---|---|
@SpringBootApplication | Entry point of a Spring Boot app |
@RestController | RESTful controller |
@RequestMapping | Map web requests |
@Autowired | Dependency injection |
@Service | Service layer component |
@Repository | Data access layer component |
REST Example
java
@RestController
@RequestMapping("/api/todos")
public class TodoController {
@GetMapping
public List<Todo> getAll() {
return todoService.findAll();
}
@PostMapping
public Todo create(@RequestBody Todo todo) {
return todoService.save(todo);
}
}