Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Mon code ce matin fonctionnait si je ne prennait pas en compte l'ObjectId
- //Je le jure sur la tête du lapin de ma copine que ça fonctionnait !!!!
- Ma classe role sans id :
- //---------------------------------------------------------------------------
- package com.aerolitec.entities;
- import com.aerolitec.database.Db;
- import com.google.gson.Gson;
- import com.mongodb.BasicDBObject;
- import com.mongodb.DBCollection;
- import com.mongodb.DBCursor;
- import com.mongodb.DBObject;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by Damien on 25/08/2014.
- */
- //Admin,User,etc....
- public class Role {
- private String name;
- private String description;
- public Role() {
- }
- public boolean add(){
- DBCollection coll = Db.databse.getCollection("Roles");
- DBObject r = new BasicDBObject();
- r.put("name",this.getName());
- DBObject result = coll.findOne(r);
- if(result != null){
- System.out.print("Nop");
- return false;
- }else{
- r.put("description",this.getDescription());
- coll.save(r);
- return true;
- }
- }
- public static List<Role> getAll(){
- Gson gson = new Gson();
- List<Role> toReturn = new ArrayList<Role>();
- DBCollection coll = Db.databse.getCollection("Roles");
- DBCursor cursor = coll.find();
- try {
- while(cursor.hasNext()) {
- System.out.print(cursor.next());
- Role r = gson.fromJson(cursor.next().toString(),Role.class);
- toReturn.add(r);
- }
- } finally {
- cursor.close();
- }
- return toReturn;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- }
- //----------------------------------------------------------
- Mon servlet pour gérer les roles :
- //----------------------------------------------------------
- package com.aerolitec.servlets;
- import com.aerolitec.entities.Role;
- import com.google.gson.Gson;
- import javax.ws.rs.*;
- import javax.ws.rs.core.Response;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by Damien on 29/08/2014.
- */
- @Path("/role")
- public class RoleServlet {
- @POST
- @Path("/add")
- public Response add(@FormParam("name") String name, @FormParam("description") String description){
- if(name != null){
- Role r_to_rec = new Role();
- r_to_rec.setName(name);
- if(description != null){
- r_to_rec.setDescription(description);
- }
- if(r_to_rec.add()){
- return Response.status(201).build();
- }else{
- return Response.status(418).build();
- }
- }else{
- return Response.status(404).build();
- }
- }
- @GET
- @Path("/")
- public Response getAll(){
- List<Role> roleList = new ArrayList<Role>();
- roleList = Role.getAll();
- String json = new Gson().toJson(roleList);
- return Response.status(200).entity(json).build();
- }
- }
- //-------------------------------------------------------------------
- Le /add fonctionne trés bien.
- Le / en get ne fonctionne plus.
- Je pense que c'est coté GSON.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement