Google App Engine End Point et Objectify
Salut à tous j'ai commencé à me mettre a GAE pour faire une api mais la je bloque.
J'ai 2 entités une Personnage et Arme (comme dans le tutoriel ) .
J'ai donc créer un api qui permet d'ajouter une arme dans le DataStore.
Code:
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
| @Api(name = "arme", version = "v1.01", scopes = { Constants.EMAIL_SCOPE }, clientIds = {
Constants.WEB_CLIENT_ID, Constants.ANDROID_CLIENT_ID,
Constants.IOS_CLIENT_ID }, audiences = { Constants.ANDROID_AUDIENCE })
public class ArmeAPI {
@ApiMethod(name = "add")
public Arme addArme(Arme a) {
Personnage p = new Personnage("kalagan",3);
Key<Personnage> r = ofy().save().entity(p).now();
Arme arme = new Arme("Epee", 33,r);
arme.setParent(r);
Key<Arme> result = ofy().save().entity(arme).now();
return ofy().load().key(result).now();
}
@ApiMethod(name = "list")
public List<Arme> getArmeList() {
return ofy().load().type(Arme.class).list();
}
} |
J'ai donc tester de l'écrire en dur puis ensuite récuperer la liste pour voir si les données on bien été sauvegarder. Le problème c'est qu'il ma sauvegardé que les attributs "Epee" et 33 . L'object Key<Personnage> il ne la pas sauvergardé...
Voici les 2 classes:
Personnage:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Entity
@Index
public class Personnage {
@Id String id;
@Unindex String nom;
int niveau;
int vie;
private Personnage() {}
public Personnage (String nom, int niveau) {
this.id = nom;
this.nom = nom;
this.niveau = niveau;
this.vie = 100;
}
} |
Arme:
Code:
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
| @Entity
public class Arme {
@Id
Long id;
@Parent
Key<Personnage> parent;
String nom;
int degats;
private Arme() {
}
public Arme(String nom, int degats, Key<Personnage> parent) {
this.nom = nom;
this.degats = degats;
this.parent = parent;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
public Key<Personnage> getParent() {
return parent;
}
@ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
public void setParent(Key<Personnage> parent) {
this.parent = parent;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public int getDegats() {
return degats;
}
public void setDegats(int degats) {
this.degats = degats;
}
} |
NB: je suis obligé de mettre @ApiResourceProperty(ignored = AnnotationBoolean.TRUE). Sinon j'ai cette erreur: Parameterized type com.googlecode.objectify.Key<com.workfel.testapi.entity.Personnage> not supported.
De plus dans ma methode REST addArme je demande un object arme en entré.
Donc dans mon body POST j'ai ça:
Code:
1 2 3 4 5 6 7 8 9 10 11
|
{
"degats": 44,
"nom": "arrow",
"parent" : {
"id" : "test",
"nom": "le nom",
"niveau" : 1,
"vie" : 9
}
} |
Mais quand je debug à la ligne le parametre a (Arme) de la méthode . Je n'ai pas l'attribut parent qui est setter.
Seulement degats et nom...
Avez vous des idées ?
Merci d'avance