简体   繁体   中英

getting null in Object passing JSON to @POST using Apache CXF

I am using Json to get object details in my POST method to save it in database. The id field is stored correctly but the rest are just taken as null. Can someone point out why.

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.ws.rs.GET;
import javax.ws.rs.*;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.ocpsoft.example.hibernate.model.SimpleObject;
import org.ocpsoft.example.hibernate.util.EntityManagerUtil;
import org.ocpsoft.example.hibernate.dao.SimpleObjectDao;
@Stateless
@Path("/pid")
public class Service {
    @GET
    @Path("{id}")
    public String get(@PathParam("id") long id){
        System.out.println("in GET request");
        SimpleObjectDao objDao=new SimpleObjectDao();
        objDao.startConnection();

        SimpleObject obj=objDao.find(id);
        objDao.closeConnection();
        Gson gson=new Gson();
        return gson.toJson(obj);
    }


@POST
@Path("/add")
@Consumes("application/json")
//@Produces({MediaType.APPLICATION_JSON})
public String postdata(SimpleObject obj)
{
    //SimpleObject obj = (SimpleObject) ob;
    //Gson gson=new Gson();
    //System.out.println(obj.toString());
    //System.out.println("object "+obj.toString());
    SimpleObjectDao objDao=new SimpleObjectDao();
    objDao.startConnection();
    objDao.save(obj);
    objDao.closeConnection();
    return "Success";
}
}

My object definition is

    package org.ocpsoft.example.hibernate.model;
import javax.persistence.*;
import java.io.Serializable;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Column;
import javax.persistence.Version;
import javax.xml.bind.annotation.XmlRootElement;

import java.lang.Override;


/**
 * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
 */
@Entity
@XmlRootElement(name="SimpleObject")
public class SimpleObject implements Serializable
{  

   private static final long serialVersionUID = -2862671438138322400L;

   @Id
   //@GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "id", updatable = false, nullable = false)
   private Long id = null;




   //private int Pid;

   @Column(name= "Pname")
   private String Pname = null;

   @Column(name= "Pcost")
   private double Pcost = 0.0f;

   @Column(name= "Pcat")
   private String Pcat = null;




   public Long getId()
   {
      return this.id;
   }



   public String getPname() {
        return Pname;
    }

    public void setPname(String pname) {
        Pname = pname;
    }

    public double getPcost() {
    return Pcost;
    }

public void setPcost(double pcost) {
    Pcost = pcost;
}

public String getPcat() {
    return Pcat;
}

public void setPcat(String pcat) {
    Pcat = pcat;
}

   public void setId(final Long id)
   {
      this.id = id;
   }



   public String toString()
   {
      String result = "";
      if (id != null)
         result += id;
      return result;
   }

   @Override
   public boolean equals(Object that)
   {
      if (this == that)
      {
         return true;
      }
      if (that == null)
      {
         return false;
      }
      if (getClass() != that.getClass())
      {
         return false;
      }
      if (id != null)
      {
         return id.equals(((SimpleObject) that).id);
      }
      return super.equals(that);
   }

   @Override
   public int hashCode()
   {
      if (id != null)
      {
         return id.hashCode();
      }
      return super.hashCode();
   }
}

Apart from Id all the others are not transferred to the object. com where I am going wrong.

I am checking using chrome postman with json as { "SimpleObject" :{"id":1,"Pname":"watch","Pcost":200.12,"Pcat":"wearables"}}

Maybe your ID column is auto generate, I think values on POST method cannot cast to your object Try this :

@POST
@Path("/add")
@Consumes("application/x-www-form-urlencoded")
@Produces(MediaType.APPLICATION_JSON)
public String postdata(MultivaluedMap<String, String> formParams)

we need build SimpleObject object from form params. hope this help !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM