Spring Boot - Exemple complet DTO,
Entity, Mapper, Service, Controller
Ce document explique la différence entre DTO et Entity, puis montre un exemple complet
d'inscription d'étudiant avec les classes : Student, InscriptionStudent, ClasseRoom, Montant,
AnneeScolaire.
1. DTO : InscriptionStudentDTO
Le DTO (Data Transfer Object) transporte uniquement les données nécessaires pour
l’inscription. Il ne contient pas d’objets entiers (Student, ClasseRoom, etc.) mais seulement
leurs identifiants.
@Data
public class InscriptionStudentDTO {
private Long id;
private Long studentId;
private Long classeRoomId;
private Long montantId;
private Long anneeScolaireId;
private LocalDate dateInscription;
}
2. Mapper : InscriptionStudentMapper
Le Mapper sert à convertir un objet Entity en DTO et inversement. Voici un exemple :
public class InscriptionStudentMapper {
// Entity → DTO
public static InscriptionStudentDTO toDTO(InscriptionStudent entity) {
InscriptionStudentDTO dto = new InscriptionStudentDTO();
[Link]([Link]());
[Link]([Link]().getId());
[Link]([Link]().getId());
[Link]([Link]().getId());
[Link]([Link]().getId());
[Link]([Link]());
return dto;
}
// DTO → Entity
public static InscriptionStudent toEntity(InscriptionStudentDTO dto) {
InscriptionStudent entity = new InscriptionStudent();
[Link]([Link]());
[Link]([Link]());
Student student = new Student();
[Link]([Link]());
[Link](student);
ClasseRoom classe = new ClasseRoom();
[Link]([Link]());
[Link](classe);
Montant montant = new Montant();
[Link]([Link]());
[Link](montant);
AnneeScolaire annee = new AnneeScolaire();
[Link]([Link]());
[Link](annee);
return entity;
}
}
3. Service : InscriptionStudentService
@Service
public class InscriptionStudentService {
private final StudentRepository studentRepo;
private final ClasseRoomRepository classeRepo;
private final MontantRepository montantRepo;
private final AnneeScolaireRepository anneeRepo;
private final InscriptionStudentRepository inscriptionRepo;
public InscriptionStudentService(StudentRepository studentRepo,
ClasseRoomRepository classeRepo,
MontantRepository montantRepo,
AnneeScolaireRepository anneeRepo,
InscriptionStudentRepository inscriptionRepo) {
[Link] = studentRepo;
[Link] = classeRepo;
[Link] = montantRepo;
[Link] = anneeRepo;
[Link] = inscriptionRepo;
}
public InscriptionStudentDTO inscrire(StudentDTO studentDTO, Long classeId, Long
montantId, Long anneeId) {
// 1. Sauvegarder l'étudiant
Student student = [Link](studentDTO);
Student savedStudent = [Link](student);
// 2. Créer l’inscription
InscriptionStudent inscription = new InscriptionStudent();
[Link](savedStudent);
[Link]([Link](classeId).orElseThrow());
[Link]([Link](montantId).orElseThrow());
[Link]([Link](anneeId).orElseThrow());
[Link]([Link]());
InscriptionStudent saved = [Link](inscription);
return [Link](saved);
}
}
4. Controller : InscriptionStudentController
@RestController
@RequestMapping("/api/inscriptions")
public class InscriptionStudentController {
private final InscriptionStudentService service;
public InscriptionStudentController(InscriptionStudentService service) {
[Link] = service;
}
@PostMapping
public InscriptionStudentDTO inscrire(@RequestBody StudentDTO studentDTO,
@RequestParam Long classeId,
@RequestParam Long montantId,
@RequestParam Long anneeId) {
return [Link](studentDTO, classeId, montantId, anneeId);
}
}
5. Exemple de requête JSON (POST)
Voici un exemple de requête JSON pour inscrire un étudiant avec Postman :
POST /api/inscriptions?classeId=1&montantId=2&anneeId=3
Body (JSON) :
{
"lastNameStudent": "Doe",
"firstNameStudent": "John",
"dateOfBirth": "2010-05-12",
"gender": "MASCULIN",
"ecolePrecedente": "Ecole Sainte-Marie"
}