GAE Jersey RESTful服务POST JSON

Kev*_*vin 4 java eclipse rest google-app-engine jersey

我正在尝试编写一个服务,它将在POST请求中接受JSON或XML对象.我已经成功编写了一个GET请求处理程序,它将按照标题的accept中的请求将我的对象作为XML或JSON返回.当我使用JSON作为请求主体POST到服务时,我的POST方法中的Java对象没有填充json中的值.

@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();
    }

}
Run Code Online (Sandbox Code Playgroud)

当我在POST方法中断时,User类型的Java对象"user"具有属性的空值.该对象本身不是null,只是属性.

这是POST提交的JSON

{"user":{"logon":"kevin","password":"password","personid":"xyz"}}
Run Code Online (Sandbox Code Playgroud)

这是我的课

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;
 }
Run Code Online (Sandbox Code Playgroud)

Kev*_*vin 5

也许没有人回答我的问题,因为问题有这么明显的解决方案!

我发现自己的错误后,我可以回答我自己的问题.

我提交的JSON正文被格式化为User对象列表,所以如果我编辑

{"user":{"logon":"kevin","password":"password","personid":"xyz"}}
Run Code Online (Sandbox Code Playgroud)

{"logon":"kevin","password":"password","personid":"xyz"}
Run Code Online (Sandbox Code Playgroud)

一切正常,因为我的@POST处理程序不期望一个User对象列表.另外,我可以调整我的@POST处理程序来接受List<User>参数!