Symfony JMS反序列化未检索对象值

Symfony JMS反序列化未检索对象值,symfony,doctrine,jmsserializerbundle,Symfony,Doctrine,Jmsserializerbundle,我正在使用Doctrine、symfony2.4、FOSRestBundle和JMSBundle创建一个简单的RESTAPI。 我创建了一个简单的用户实体和一个控制器来实现POST方法(GET方法很好)。当我试图发布数据时,我的$user对象属性始终为空,请求->数据上的值没有被反序列化。。一整天都在为此奋斗真的 我的用户实体: namespace AbcBank\ApiBundle\Entity; use Doctrine\ORM\Mapping as ORM; use JMS\Serial

我正在使用Doctrine、symfony2.4、FOSRestBundle和JMSBundle创建一个简单的RESTAPI。 我创建了一个简单的用户实体和一个控制器来实现POST方法(GET方法很好)。当我试图发布数据时,我的$user对象属性始终为空,请求->数据上的值没有被反序列化。。一整天都在为此奋斗真的

我的用户实体:

namespace AbcBank\ApiBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Accessor;

/**
 * User
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AbcBank\ApiBundle\Entity\UserRepository")
 * @JMS\ExclusionPolicy("all")
 */
class User
{
    /**
     * @var integer
     * 
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * 
     * @Expose
     * @JMS\Type("integer")
     * @Accessor(getter="getId")
     */
    private $id;

    /**
     * @var string
     * 
     * @ORM\Column(name="username", type="string", length=25)
     * 
     * @Expose
     * @JMS\Type("string")
     */
    private $username;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set username
     *
     * @param string $username
     * @return User
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    /**
     * Get username
     *
     * @return string 
     */
    public function getUsername()
    {
        return $this->username;
    }
}
然后在我的用户控制器(src/ApiBundle/Controller/UserController.php)中,我有: 命名空间AbcBank\ApiBundle\Controller

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations\View;
use JMS\Serializer\SerializerBuilder;

use AbcBank\ApiBundle\Entity\User;

/**
 * User controller.
 *
 * @Route("/user")
 */
class UserController extends Controller
{
    //... more code

    /**
     * POST
     * 
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @return FormTypeInterface|View
     */
    public function postUserAction(Request $request)
    {
        $serializer = $this->get('jms_serializer');

        /**
         * deserialize the json content to a User type object
         */
        $content = $request->getContent();
        $user = $serializer->deserialize($content, 'AbcBank\ApiBundle\Entity\User', 'json');

        if ($user instanceof \AbcBank\ApiBundle\Entity\User === false) {
            return View::create(array('errors' => $user), 400);
        }

        /**
         * persist object into the database
         */
        $em = $this->getDoctrine()->getManager();
        $em->persist($user);
        $em->flush();

        $url = $this->generateUrl(
            'user_get',
            array('id' => $user->getId()),
            true
        );

        $response = new Response();
        $response->setStatusCode(201);
        $response->headers->set('Location', $url);

        return $response;   
    }
}
如有任何帮助,我将不胜感激。。非常非常令人沮丧,可能是我错过了一些愚蠢的东西

亲切问候。

试试这个:

public function postUserAction(Request $request) {
$serializer = $this->get('jms_serializer');

/**
 * deserialize the json content to a User type object
 */
$content = $request->getContent();
$user = $serializer->deserialize($content, 'AbcBank\ApiBundle\Entity\User', 'json');

if ($user instanceof \AbcBank\ApiBundle\Entity\User === false) {
    return View::create(array('errors' => $user), 400);
}

/**
 * persist object into the database
 */
$em = $this->getDoctrine()->getManager();

$updatedUser = $em->merge($user);

$em->flush();

$url = $this->generateUrl(
    'user_get',
    array('id' => $user->getId()),
    true
);

$response = new Response();
$response->setStatusCode(201);
$response->headers->set('Location', $url);

return $response };   

这可能是因为序列化程序在默认情况下将属性从大小写转换为“小写下划线名称”,而在反序列化时则相反

序列化:myProperty=>my_属性
desriaze:my_属性=>myProperty


参见文档中的

您解决过这个问题吗?我也有同样的问题,我想,反序列化对象中的所有字段都是空的,尽管JSON字符串中有值。我和Dai有同样的问题。只有“我的Id”字段填写正确,其余字段留空。欢迎有任何见解。