0% ont trouvé ce document utile (0 vote)
7 vues3 pages

API REST avec Spring Boot

Transféré par

Mohamedamine Houidi
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats TXT, PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
7 vues3 pages

API REST avec Spring Boot

Transféré par

Mohamedamine Houidi
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats TXT, PDF, TXT ou lisez en ligne sur Scribd

D'accord !

Voici un exemple simple d'application Spring Boot qui expose une API
REST pour gérer une liste de tâches ("To-Do List").

### Étape 1 : Créer un projet avec Spring Initializr


- Accédez à [Spring Initializr]([Link]
- Choisissez les options suivantes :
- **Dependencies** : Spring Web, Spring Data JPA, H2 Database.
- **Packaging** : Jar.
- **Language** : Java.
- Téléchargez et extrayez le projet.

---

### Étape 2 : Codez votre application

#### 1. **Entité JPA**


```java
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];

@Entity
public class Todo {
@Id
@GeneratedValue(strategy = [Link])
private Long id;
private String title;
private boolean completed;

// Getters and Setters


public Long getId() {
return id;
}

public void setId(Long id) {


[Link] = id;
}

public String getTitle() {


return title;
}

public void setTitle(String title) {


[Link] = title;
}

public boolean isCompleted() {


return completed;
}

public void setCompleted(boolean completed) {


[Link] = completed;
}
}
```
---

#### 2. **Repository**
```java
package [Link];

import [Link];
import [Link];

public interface TodoRepository extends JpaRepository<Todo, Long> {


}
```

---

#### 3. **Controller**
```java
package [Link];

import [Link];
import [Link];
import [Link].*;

import [Link];

@RestController
@RequestMapping("/api/todos")
public class TodoController {

private final TodoRepository todoRepository;

public TodoController(TodoRepository todoRepository) {


[Link] = todoRepository;
}

@GetMapping
public List<Todo> getAllTodos() {
return [Link]();
}

@PostMapping
public Todo createTodo(@RequestBody Todo todo) {
return [Link](todo);
}

@PutMapping("/{id}")
public Todo updateTodo(@PathVariable Long id, @RequestBody Todo updatedTodo) {
return [Link](id).map(todo -> {
[Link]([Link]());
[Link]([Link]());
return [Link](todo);
}).orElseThrow(() -> new RuntimeException("Todo not found with id " + id));
}

@DeleteMapping("/{id}")
public void deleteTodo(@PathVariable Long id) {
[Link](id);
}
}
```

---

#### 4. **[Link]**
Ajoutez les paramètres pour la base de données H2 dans
`src/main/resources/[Link]` :
```properties
[Link]=jdbc:h2:mem:testdb
[Link]=[Link]
[Link]=sa
[Link]=password
[Link]-platform=[Link].H2Dialect
[Link]=true
```

---

### Étape 3 : Lancez l'application


1. Exécutez la classe principale générée (par défaut `[Link]`).
2. Accédez à l'API REST avec des outils comme Postman ou cURL :
- **GET** `/api/todos`
- **POST** `/api/todos` avec un corps JSON :
```json
{
"title": "Learn Spring Boot",
"completed": false
}
```
- **PUT** `/api/todos/{id}` pour mettre à jour une tâche.
- **DELETE** `/api/todos/{id}` pour supprimer une tâche.

---

C'est une base pour démarrer rapidement. Si vous voulez ajouter des fonctionnalités
ou avez une question, dites-moi ! 😊

Vous aimerez peut-être aussi