2015-11-18 51 views
0

我想在使用java的服務器中執行POST操作。json中的發佈請求RESTful

它正在使用xml,但不使用json。你可以幫我嗎? 這個例子效果很好。

config = new ClientConfig(); 
client = ClientBuilder.newClient(config); 
target = client.target("http://192.168.1.156:5700/sdelab07-local").path("person"); 

String xmlRequest = "<person><firstname>ffff</firstname><healthProfile><value>89</value></healthProfile></person>"; 

response = target.request(MediaType.APPLICATION_XML_TYPE).post(Entity.xml(xmlRequest)); 
String xmlLine = response.readEntity(String.class); 
System.out.println(xmlLine); 

這個json的POST請求不起作用,爲什麼?

String jsonRequest = "{\"firstname\" : \"ffff\"," + 
          "\"healthProfile\" : {" + 
          "\"value\" : 51}}"; 

response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(jsonRequest)); 
String jsonLine = response.readEntity(String.class); 
System.out.println(jsonLine); 

我得到以下錯誤:

N/A (through reference chain: introsde.rest.ehealth.model.Person["healthProfile"])

我覺得我jsonRequest是在一個錯誤的格式。如果是這樣,那麼正確的應該是什麼?

我試圖現在在郵差中發佈郵件請求。身份證即使在那裏也不起作用。我在使用xml發佈時沒有問題。 使用JSON我得到以下信息(在郵差):

Can not deserialize instance of introsde.rest.ehealth.model.LifeStatus out of START_ARRAY token at [Source: org.glassfish.jersey.me[email protected]208dd233; line: 5, column: 31] (through reference chain: introsde.rest.ehealth.model.Person["healthProfile"])

這是我的Person類:

> @Entity // indicates that this class is an entity to persist in DB 
> @Table(name="Person") // to whole table must be persisted 
> @NamedQuery(name="Person.findAll", query="SELECT p FROM Person p") 
> @XmlType(propOrder={"idPerson", "name", "lastname", "birthdate", 
> "email", "username", "lifeStatus"}) @XmlRootElement 
> 
> public class Person implements Serializable { 
>  private static final long serialVersionUID = 1L; 
>  @Id // defines this attributed as the one that identifies the entity 
>  @GeneratedValue(generator="sqlite_person") 
>  @TableGenerator(name="sqlite_person", table="sqlite_sequence", 
>   pkColumnName="name", valueColumnName="seq", 
>   pkColumnValue="Person") 
>  @Column(name="idPerson") 
>  private int idPerson; 
>  @Column(name="lastname") 
>  private String lastname; 
>  @Column(name="name") 
>  private String name; 
>  @Column(name="username") 
>  private String username; 
>  //@Temporal(TemporalType.DATE) // defines the precision of the date attribute 
>  @Column(name="birthdate") 
>  private String birthdate; 
>  @Column(name="email") 
>  private String email; 
>  
>  // mappedBy must be equal to the name of the attribute in healthProfile that maps this relation 
>  @XmlElement(name="healthProfile") 
>  @OneToMany(mappedBy="person",cascade=CascadeType.ALL,fetch=FetchType.EAGER) 
>  private List<LifeStatus> lifeStatus; 
>  
>  //@XmlElementWrapper(name = "Measurements") 
>  //@XmlElement(name="lifeStatus") 
>  public List<LifeStatus> getLifeStatus() { 
>   return lifeStatus; 
>  } 
>  
>  public void setLifeStatus(LifeStatus newLS){ 
>  this.lifeStatus.add(newLS); 
>  } 
>  
>  // mappedBy must be equal to the name of the attribute in HealthMeasureHistory that maps this relation 
>  @OneToMany(mappedBy="person",cascade=CascadeType.ALL,fetch=FetchType.EAGER) 
>  private List<HealthMeasureHistory> healthMeasureHistory; 
>  
>  //@XmlElementWrapper(name = "measureHistory") 
>  //@XmlElement(name = "measure") 
>  @XmlTransient 
>  public List<HealthMeasureHistory> getHealthMeasureHistories() { 
>   return healthMeasureHistory; 
>  } 
>  
>  
>  
>  // add below all the getters and setters of all the private attributes 
>  public Person() {} 
>  // getters 
>  //@XmlTransient 
>  public int getIdPerson(){ 
>   return idPerson; 
>  } 
> 
>  public String getLastname(){ 
>   return lastname; 
>  } 
>  
>  @XmlElement(name="firstname") 
>  public String getName(){ 
>   return name; 
>  } 
>  public String getUsername(){ 
>   return username; 
>  } 
>  public String getBirthdate(){ 
>   return birthdate; 
>  } 
>  public String getEmail(){ 
>   return email; 
>  } 
>  
>  // setters 
>  public void setIdPerson(int idPerson){ 
>   this.idPerson = idPerson; 
>  } 
>  public void setLastname(String lastname){ 
>   this.lastname = lastname; 
>  } 
>  public void setName(String name){ 
>   this.name = name; 
>  } 
>  public void setUsername(String username){ 
>   this.username = username; 
>  } 
>  public void setBirthdate(String birthdate){ 
>   this.birthdate = birthdate; 
>  } 
>  public void setEmail(String email){ 
>   this.email = email; 
>  } 
> 
>  
>  public static Person getPersonById(int personId) { 
>   EntityManager em = LifeCoachDao.instance.createEntityManager(); 
>   Person p = em.find(Person.class, personId); 
>   LifeCoachDao.instance.closeConnections(em); 
>   return p; 
>  } 
> 
>  public static List<Person> getAll() { 
>   EntityManager em = LifeCoachDao.instance.createEntityManager(); 
>   List<Person> list = em.createNamedQuery("Person.findAll", Person.class) 
>    .getResultList(); 
>   LifeCoachDao.instance.closeConnections(em); 
>   return list; 
>  } 
> 
>  public static Person savePerson(Person p) { 
>   EntityManager em = LifeCoachDao.instance.createEntityManager(); 
>   EntityTransaction tx = em.getTransaction(); 
>   tx.begin(); 
>   em.persist(p); 
>   tx.commit(); 
>   LifeCoachDao.instance.closeConnections(em); 
>   return p; 
>  } 
>  
>  public static Person savePersonWithDetail(Person p) { 
>   EntityManager em = LifeCoachDao.instance.createEntityManager(); 
>   EntityTransaction tx = em.getTransaction(); 
>   tx.begin(); 
>   em.persist(p); 
>   tx.commit(); 
>   LifeCoachDao.instance.closeConnections(em); 
>   return p; 
>  } 
> 
>  public static Person updatePerson(Person p) { 
>   EntityManager em = LifeCoachDao.instance.createEntityManager(); 
>   EntityTransaction tx = em.getTransaction(); 
>   tx.begin(); 
>   p=em.merge(p); 
>   tx.commit(); 
>   LifeCoachDao.instance.closeConnections(em); 
>   return p; 
>  } 
> 
>  public static void removePerson(Person p) { 
>   EntityManager em = LifeCoachDao.instance.createEntityManager(); 
>   EntityTransaction tx = em.getTransaction(); 
>   tx.begin(); 
>   p=em.merge(p); 
>   em.remove(p); 
>   tx.commit(); 
>   LifeCoachDao.instance.closeConnections(em); 
>  } 
>  } 

太謝謝你了!

+1

顯示你的'Person'類。 – dguay

+0

我想你可以使用json stringify。這使事情變得容易一些。並且請提供更多細節。 – user4321

+0

''healthProfile「/ lifeSatus'是一個數組。所以它應該是''healthProfile「:[{..},{..}]' –

回答

0

JSON的正確的格式是:

{ 
    "person": { 
     "firstname": "ffff", 
     "healthProfile": { "value": "89" } 
     } 
} 

希望這可以幫助你