简体   繁体   English

使用Apache CXF在将JSON传递到@POST的对象中获取null

[英]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. 我正在使用Json在POST方法中获取对象详细信息,以将其保存在数据库中。 The id field is stored correctly but the rest are just taken as null. id字段已正确存储,但其余字段仅被视为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. 除了Id之外,其他所有都不会转移到该对象。 com where I am going wrong. COM,我要去哪里错了。

I am checking using chrome postman with json as { "SimpleObject" :{"id":1,"Pname":"watch","Pcost":200.12,"Pcat":"wearables"}} 我正在使用带有json的chrome邮递员检查{{“ 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 : 也许您的ID列是自动生成的,我认为POST方法上的值无法转换为您的对象尝试以下操作:

@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. 我们需要从表单参数构建SimpleObject对象。 hope this help ! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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