Bonjour, je suis en train de créer un back end java pour mon Jeu de Rôle sous Angular.
Je n'arrive pas à comprendre mon erreur et celà depuis plusieurs heures :@
L'utilisateur va créer sa partie en lui donnant un nom et choisir l'arme, la classe et le nom de ses héros.
Pour avoir un front propre, je veux que mon back s'occupe de finaliser la création de la partie (hp, def, atk , etc...).
Requête effectué avec POSTMAN :
Mon body de ma requête HTTP :
Code source :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 { "nomPartie": "Alesia", "tabHero": [ {"nom":"svagor","classe":"guerrier","arme":"Epee"}, {"nom":"gtrhtrhtrh","classe":"soigneur","arme":"Epee"} ] }
Hero.java
Partie.Java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 package com.swbattle.models; public class Hero { String classe; String nom; int def; int hp; int hpMax; int atk; String arme; String urlIcone; public String getUrlIcone() { return urlIcone; } public void setUrlIcone(String urlIcone) { this.urlIcone = urlIcone; } public int getAtk() { return atk; } public void setAtk(int atk) { this.atk = atk; } public int getHpMax() { return hpMax; } public void setHpMax(int hpMax) { this.hpMax = hpMax; } public String getNom() { return nom; } public void setNom(String nom) { this.nom = nom; } public Hero() { super(); } public String getClasse() { return classe; } public void setClasse(String classe) { this.classe = classe; } public int getDef() { return def; } public void setDef(int def) { this.def = def; } public int getHp() { return hp; } public void setHp(int hp) { this.hp = hp; } public String getArme() { return arme; } public void setArme(String arme) { this.arme = arme; } }
PartieService.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.swbattle.models; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import lombok.Data; @Document @Data public class Partie { @Id private String id; private String nomPartie; private Hero[] tabHero; private Monstre[] tabMonstre; private int numeroManche; private int rand; }
PartieController.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 package com.swbattle.services; import org.springframework.stereotype.Service; import com.swbattle.models.Hero; @Service public class PartieService { public Hero[] creerHero(Hero[] tabHero){ System.out.println("Je suis bien dans mon service ! "); for (int i = 0; i < tabHero.length; i++) { switch (tabHero[i].getClasse()) { case "guerrier": tabHero[i].setHpMax(800); tabHero[i].setHp(800); tabHero[i].setDef(200); tabHero[i].setAtk(100); tabHero[i].setUrlIcone("./assets/images/icon_classe/guerrier.jpg"); System.out.println("J'ai créer un guerrer !"); break; case "soigneur": tabHero[i].setHpMax(600); tabHero[i].setHp(600); tabHero[i].setDef(50); tabHero[i].setAtk(50); tabHero[i].setUrlIcone("./assets/images/icon_classe/priest.jpg"); System.out.println("J'ai créer un soigneur !"); break; case "assassin": tabHero[i].setHpMax(600); tabHero[i].setHp(600); tabHero[i].setDef(50); tabHero[i].setAtk(300); tabHero[i].setUrlIcone("./assets/images/icon_classe/assassin.jpg"); System.out.println("J'ai créer un assassin !"); break; case "archer": tabHero[i].setHpMax(700); tabHero[i].setHp(700); tabHero[i].setDef(50); tabHero[i].setAtk(200); tabHero[i].setUrlIcone("./assets/images/icon_classe/archer.jpg"); break; default: break; } } return tabHero; } }
PartieRepository.Java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package com.swbattle.controllers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.swbattle.models.Hero; import com.swbattle.models.Partie; import com.swbattle.repositories.PartieRepository; import com.swbattle.services.PartieService; @CrossOrigin @RestController @RequestMapping("partie") public class PartieController { @Autowired private PartieRepository repository; private PartieService service; @PostMapping("/creer") public Partie save(@RequestBody Partie entity) { //Recuperd la taille du tableau hero int tailleTabHero = 0; tailleTabHero = entity.getTabHero().length; //Créer notre tableau hero Hero[] tabHero = new Hero[tailleTabHero]; Partie partie = new Partie(); //Recuperd les heros dans notre request body tabHero = entity.getTabHero(); //Test pour vérifier si nous récupérons bien nos héros for (int i = 0; i < tailleTabHero; i++) { System.out.println(tabHero[i]); } //Définir les stats du héro Hero[] newTabHero = new Hero[tailleTabHero]; newTabHero = this.service.creerHero(tabHero); partie.setTabHero(newTabHero); //Créer les monstres TODO //Sauvegarde la partie dans notre BDD MongoDB return this.repository.save(partie); } }
L'erreur se manifeste à la ligne 40 de PartieController.Java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 package com.swbattle.repositories; import org.springframework.data.mongodb.repository.MongoRepository; import com.swbattle.models.Partie; public interface PartieRepository extends MongoRepository<Partie, String>{ }
Je suis certains que c'est une erreur à la con.. mais là je sèche
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 Hero[] newTabHero = new Hero[tailleTabHero]; //ERREUR<br>newTabHero = this.service.creerHero(tabHero);
Merci d'avance pour vos retours.
Partager