繁体   English   中英

在JSON RESTful中发布请求

[英]Post request in json RESTful

我想在使用Java的服务器上执行POST操作。

它适用于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);

我收到以下错误:

不适用(通过参考链:introsde.rest.ehealth.model.Person [“ healthProfile”])

我认为我的jsonRequest格式错误。 如果是这样,那应该是正确的呢?

我现在尝试在Postman中发出发帖请求。 即使在那儿也无法使用ID。 我在使用xml发布时没有问题。 使用json,我得到以下消息(在邮递员中):

无法在[来源:org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@208dd233;的START_ARRAY令牌中反序列化introsde.rest.ehealth.model.LifeStatus实例。 第5行,第31列](通过参考链: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);
>     }
>      }

非常感谢!

json的正确格式为:

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

希望这可以帮到你

暂无
暂无

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

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