我正在使用Json在POST方法中獲取對象詳細信息以將其保存在數據庫中。 id字段存儲正確,但其餘的只是空值。有人可以指出原因。在使用Apache CXF將JSON傳遞到@POST的Object中獲得null null
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";
}
}
我的對象定義
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:[email protected]">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();
}
}
除ID的所有其他人都沒有轉移的對象。 com我哪裏錯了。
我使用鉻郵遞員使用JSON作爲 檢查{ 「SimpleObject」:{ 「ID」:1, 「PNAME」: 「看」, 「Pcost」:200.12, 「PCAT」: 「可穿戴」}}
一眼望去,我找不到任何映射問題。你嘗試調試setter方法嗎?什麼值被傳遞給它? –