我正在使用Doctrine,Symfony 2.4,FOSRestBundle和JMSBundle来创建一个简单的REST api。 我创建了一个简单的User实体和一个控制器来实现POST方法(GET方法工作正常)。当我尝试POST数据时,我的$ user对象属性总是为null,请求 - >数据上的值没有被反序列化。真的打了一整天..
我的用户实体:
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;
}
}
会感激任何帮助..非常非常令人沮丧,可能是我想念的傻事。
亲切的问候。答案 0 :(得分:3)
这可能是因为序列化程序将属性从驼峰式转换为低级的强调名称'默认情况下,反序列化时反过来。
序列化:myProperty => my_property
Desrialize:my_property => myProperty的
答案 1 :(得分:0)
试试这个:
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 };