[RESTfull] Difficultés avec les POST
Bonjour à tous, j'ai un mini projet à rendre sur les webservices RESTfull en java en utilisant Eclipse et Tomcat 7.0.
Le but est de créer un service pour une "chaîne de caractère" avec plusieurs méthodes:
- GET .../chainedecaractères -> retourne la chaine de caractère
- POST .../chainedecaractères/set + body nouvelle_chaine -> affecte la chaîne par la nouvelle_chaine
- GET .../chainedecaractères/3 -> renvoie le caractère en 3ème position s'il y a, sinon rien
- POST .../chainedecaractères/concat + body autre_chaine -> concatène la chaîne courante avec l'autre chaine.
Je n'ai eu aucun problème à réaliser les requêtes GET mais pour les deux POST c'est une autre histoire, je n'arrive pas à comprendre comment arriver à atteindre un résultat telle que .../chainedecaractères/set?contenu=Bonsoir (j'imagine) transforme bien le contenu de ma chaine Bonjour en Bonsoir. J'ai reagarder pas mal de discussions ou des tutos mais beaucoup utilise le rest avec des URL POST de type .../chainedecaractères/set/Bonsoir.
Je solicite donc votre aide si vous avez un petit example de POST à me proposer avec l'URL qui déclenche le POST.
Voici mes différentes classes prise dans mon Eclipse:
(Methodes REST) ChainedecaracteresRessource.java
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 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 100 101 102 103 104 105 106 107 108 109
|
package df.chainedecaracteres.miniprojet;
import javax.validation.constraints.Past;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;
import df.chainedecaracteres.miniprojet.model.DaoPhrases;
import df.chainedecaracteres.miniprojet.model.Chainedecaracteres;
@Path("/chainedecaractères")
public class ChainedecaracteresRessource {
@Context
UriInfo uriInfo;
@Context
Request request;
String id = "1";
public ChainedecaracteresRessource(UriInfo uriInfo, Request request, String id) {
super();
this.uriInfo = uriInfo;
this.request = request;
this.id = id;
}
public ChainedecaracteresRessource(){
}
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Chainedecaracteres getChainedecaracteres() {
Chainedecaracteres chaine = DaoPhrases.instance.getChainedecaracteres().get(id);
if (chaine == null)
throw new RuntimeException("Get: Chaine id " + id + " inexistant");
return chaine;
}
@GET
@Produces({MediaType.TEXT_XML})
public Chainedecaracteres getChainedecaracteresBrowser() {
Chainedecaracteres chaine = DaoPhrases.instance.getChainedecaracteres().get(id);
if (chaine == null)
throw new RuntimeException("Get: Chaine id " + id + " inexistant");
return chaine;
}
@Path("/{nombre}")
@GET
public char getcaractere(@PathParam("nombre") int nombre) {
Chainedecaracteres chaine = DaoPhrases.instance.getChainedecaracteres().get(id);
char caractere;
if (chaine == null) {
throw new RuntimeException("Get: Chaine id " + id + " inexistant"); }
else {
nombre = nombre -1;
String contenu = chaine.getContenu();
int taille = contenu.length();
if(nombre > taille) {
caractere = ' ';}
else {
caractere = contenu.charAt(nombre);
}
}
return caractere;
}
@Path("/set")
@POST
public Chainedecaracteres newContenu(@QueryParam("contenu") String contenu) {
Chainedecaracteres chaine = DaoPhrases.instance.getChainedecaracteres().get(id);
if (chaine == null) {
throw new RuntimeException("Get: Chaine id " + id + " inexistant"); }
else {
chaine.setContenu(contenu); }
return chaine;
}
@Path("/concat")
@POST
@Produces(MediaType.TEXT_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Chainedecaracteres addContenu(@FormParam("contenu") String contenu) {
String newcontenu;
Chainedecaracteres chaine = DaoPhrases.instance.getChainedecaracteres().get(id);
if (chaine == null) {
throw new RuntimeException("Get: Chaine id " + id + " inexistant"); }
else {
newcontenu = chaine.getContenu();
newcontenu = newcontenu.concat(contenu);
chaine.setContenu(newcontenu);
}
return chaine;
}
} |
(Objet Chaine) Chainedecaracteres.java
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
| package df.chainedecaracteres.miniprojet.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Chainedecaracteres {
public Chainedecaracteres() {
super();
this.contenu = null;
this.id = null;
}
public Chainedecaracteres(String id, String contenu)
{
super();
this.contenu = contenu;
this.id = id;
}
private String contenu;
private String id;
public String getContenu(){
return contenu;
}
public void setContenu(String contenu){
this.contenu = contenu;
}
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
} |
(Instance) DaoPhrases.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package df.chainedecaracteres.miniprojet.model;
import java.util.HashMap;
import java.util.Map;
public enum DaoPhrases {
instance;
private Map<String, Chainedecaracteres> contentProvider = new HashMap<String, Chainedecaracteres>();
private DaoPhrases() {
Chainedecaracteres chaine = new Chainedecaracteres("1","Bonjour");
contentProvider.put(chaine.getId(),chaine);
}
public Map<String, Chainedecaracteres> getChainedecaracteres() {
return contentProvider;
}
} |