我想在使用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);
我收到以下错误:
N / A(通过参考链:introsde.rest.ehealth.model.Person [“healthProfile”])
我认为我的jsonRequest格式错误。如果是这样,那该怎么办?
我现在试着在Postman发帖子。 Id甚至不起作用。使用xml发布时没有问题。 使用json,我收到以下消息(在Postman中):
无法反序列化introsde.rest.ehealth.model.LifeStatus的实例 在[来源:来自:START_ARRAY标记 org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@208dd233; line:5,column: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);
> }
> }
非常感谢你!
答案 0 :(得分:0)
正确的json格式是:
{
"person": {
"firstname": "ffff",
"healthProfile": { "value": "89" }
}
}
希望这可以帮到你