🌱 Qu'est-ce que Spring Boot ?
Spring Boot est un framework Java basé sur Spring qui permet de :
-
Créer des applications web ou API REST très rapidement
-
Éviter les configurations manuelles (via l’auto-configuration)
-
Démarrer avec un serveur embarqué (comme Tomcat)
-
Être prêt pour la production avec peu d'effort
👉 Spring Boot suit le principe "convention over configuration".
✅ Caractéristiques clés de Spring Boot
-
Auto-configuration des beans et des dépendances
-
Démarrage rapide (
main()avec@SpringBootApplication) -
Serveur web intégré (Tomcat, Jetty, etc.)
-
Fichiers de configuration simplifiés (
application.propertiesou.yml) -
Support de REST, JPA, sécurité, tests, etc.
🔷 Annotations (@) les plus utilisées
| Annotation | Description | Exemple |
|---|---|---|
@SpringBootApplication |
Démarre une application Spring Boot (inclut @Configuration, @EnableAutoConfiguration, @ComponentScan) |
@SpringBootApplication public class MyApp {} |
@RestController |
Déclare une classe comme contrôleur REST | @RestController public class MyController {} |
@RequestMapping, @GetMapping, @PostMapping, etc. |
Associent des méthodes à des routes HTTP | @GetMapping("/users") |
@Autowired |
Injection automatique d’un bean (dépendance) | @Autowired UserService service; |
@Service, @Repository, @Component |
Déclarent des beans Spring (logique métier, DAO, composants) | @Service class UserService {} |
@Entity |
Marque une classe comme entité JPA (liée à une table BD) | @Entity class User {} |
@Id |
Clé primaire de l’entité | @Id Long id; |
@GeneratedValue |
Auto-génère l’identifiant | @GeneratedValue(strategy = GenerationType.IDENTITY) |
@PathVariable |
Extrait une valeur d’un paramètre de l’URL | /users/{id} → @PathVariable Long id |
@RequestParam |
Récupère les paramètres de la requête GET | /search?q=java → @RequestParam String q |
@RequestBody |
Lie le corps JSON d’une requête à un objet Java | @PostMapping → public void add(@RequestBody User u) |
@Valid |
Valide les données d’entrée via des annotations JSR 380 | @Valid User user |
@Configuration |
Déclare une classe de configuration manuelle | @Configuration class AppConfig {} |
@Bean |
Définit un bean manuellement dans @Configuration |
@Bean public DataSource ds() {} |
🔧 Exemple minimal Spring Boot REST API
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@RestController
@RequestMapping("/hello")
class HelloController {
@GetMapping
public String sayHello() {
return "Hello, Spring Boot!";
}
}
✅ Résumé
| Categorie | Annotations typiques |
|---|---|
| Application Spring | @SpringBootApplication, @ComponentScan |
| Contrôleurs REST | @RestController, @GetMapping, @PostMapping, @PathVariable, @RequestParam, @RequestBody |
| Injection de dépendance | @Autowired, @Service, @Repository, @Component |
| Accès base de données | @Entity, @Id, @GeneratedValue, @Table, @Column |
| Configuration | @Configuration, @Bean |
| Validation | @Valid, @NotNull, @Size, @Email |
Aucun commentaire:
Enregistrer un commentaire