簡體   English   中英

使用Apache CXF在將JSON傳遞到@POST的對象中獲取null

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

我正在使用Json在POST方法中獲取對象詳細信息,以將其保存在數據庫中。 id字段已正確存儲,但其余字段僅被視為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: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();
   }
}

除了Id之外,其他所有都不會轉移到該對象。 COM,我要去哪里錯了。

我正在使用帶有json的chrome郵遞員檢查{{“ SimpleObject”:{“ id”:1,“ Pname”:“ watch”,“ Pcost”:200.12,“ Pcat”:“ wearables”}}

也許您的ID列是自動生成的,我認為POST方法上的值無法轉換為您的對象嘗試以下操作:

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

我們需要從表單參數構建SimpleObject對象。 希望對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM