Java GAE Jersey RESTful服务POST JSON
我正在尝试编写一个服务,它将在POST请求中接受JSON或XML对象。我已经成功地编写了一个GET请求处理程序,该处理程序将按照头部的accept中的请求以XML或JSON的形式返回我的对象。当我将JSON作为请求主体发布到服务时,POST方法中的Java对象不会被JSON中的值填充Java GAE Jersey RESTful服务POST JSON,java,eclipse,google-app-engine,rest,jersey,Java,Eclipse,Google App Engine,Rest,Jersey,我正在尝试编写一个服务,它将在POST请求中接受JSON或XML对象。我已经成功地编写了一个GET请求处理程序,该处理程序将按照头部的accept中的请求以XML或JSON的形式返回我的对象。当我将JSON作为请求主体发布到服务时,POST方法中的Java对象不会被JSON中的值填充 @POST @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public void postUser(@Context Http
@POST
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void postUser(@Context HttpServletRequest Req, User user)
{
PersistenceManager pm = PMF.get().getPersistenceManager();
try
{
pm.makePersistent(user);
}
finally
{
pm.close();
}
}
当我打断POST方法时,user类型的Java对象“user”的属性值为null。对象本身不是null,只是属性
这是通过POST提交的JSON
{"user":{"logon":"kevin","password":"password","personid":"xyz"}}
这是我的班级
package com.afalon.cloud.contracts;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
@PersistenceCapable
@XmlRootElement(name = "user")
@XmlAccessorType(XmlAccessType.NONE)
public class User {
@Persistent
@XmlElement(name="logon")
private String logon;
@Persistent
@XmlElement(name="password")
private String password;
@Persistent
@XmlElement(name="personid")
private String personid;
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
@XmlElement(name="userid")
private String userid;
public User () {}
public void setLogOn(String value) {
logon = value;
}
public String getLogOn() {
return logon;
}
public void setPassword(String value) {
password = value;
}
public String getPassword() {
return password;
}
public void setPersonId(String value) {
personid = value;
}
public String getPersonId() {
return personid;
}
public String getUserId() {
return userid;
}
也许没有人回答我的问题,因为这个问题有如此明显的解决办法 在我注意到我的错误后,我可以回答我自己的问题 我提交的JSON正文被格式化为
User
对象的列表,因此如果我编辑
{"user":{"logon":"kevin","password":"password","personid":"xyz"}}
到
一切正常,因为我的@POST处理程序不需要
User
对象列表。或者,我可以调整@POST处理程序以接受列表
参数 实际上,您正在发布一个具有user
属性且包含“user”的对象。对象由{}表示,数组由[]表示。没有清单本身。
{"logon":"kevin","password":"password","personid":"xyz"}