I want to do a POST operation in a server using java.
It is working with xml but not with json. Can you help me? This example works good.
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);
This POST request with json does not work good, why?
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);
I get the following error:
N/A (through reference chain: introsde.rest.ehealth.model.Person["healthProfile"])
I think my jsonRequest is in a wrong format. If so, how should be the right one?
I tried to make a post request in Postman now. Id does not work even there. I do not have problems on posting using xml. With json I get the following message (in Postman):
Can not deserialize instance of introsde.rest.ehealth.model.LifeStatus out of START_ARRAY token at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@208dd233; line: 5, column: 31] (through reference chain: introsde.rest.ehealth.model.Person["healthProfile"])
This is my Person class:
> @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);
> }
> }
Thank you so much!
the right format of json is:
{
"person": {
"firstname": "ffff",
"healthProfile": { "value": "89" }
}
}
hope this can help you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.