[英]Could not denormalize object of type, no supporting normalizer found. Symfony 2.8
I'm trying to serialize and deserialize between json and doctrine entity but keep getting this error我正在尝试在 json 和学说实体之间进行序列化和反序列化,但不断收到此错误
"Could not denormalize object of type AppBundle/Person, no supporting normalizer found. "
“无法对 AppBundle/Person 类型的对象进行非规范化,找不到支持的规范化器。”
My entity class我的实体类
<?php
namespace AppBundle\Entity;
class Person
{
private $age;
private $name;
// Getters
public function getName()
{
return $this->name;
}
public function getAge()
{
return $this->age;
}
// Setters
public function setName($name)
{
$this->name = $name;
}
public function setAge($age)
{
$this->age = $age;
}
}
My controller我的控制器
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\Person;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
class RawGrnController extends Controller
{
public function newAction(Request $request)
{
$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new ObjectNormalizer(),new GetSetMethodNormalizer());
$serializer = new Serializer($normalizers, $encoders);
$data =$data = <<<EOF
<person>
<name>foo</name>
<age>99</age>
</person>
EOF;
$person = $serializer->deserialize($data, 'AppBundle\Person', 'xml');
$reaponse = new Response($person);
return $reaponse;
}
}
I got this code from the official Symfony documentation Here .I have tried to use this in my real project and got the above error then, I tried the example on the documentation but still getting the same error.我从官方 Symfony 文档这里得到了这个代码。我试图在我的真实项目中使用它,然后得到了上面的错误,我尝试了文档中的例子,但仍然得到同样的错误。
Things I've tried so far到目前为止我尝试过的事情
I have no clue what I'm missing to get the normalizer to work.我不知道我缺少什么才能使规范化器工作。 Objects in my project are far more complex but I'm getting bit suspicious as the example in documentation gives the same error.
我项目中的对象要复杂得多,但我有点怀疑,因为文档中的示例给出了相同的错误。 Any help is highly appreciated.
任何帮助都受到高度赞赏。
For future generations, if you inject the serializer into the controller in order to deserialize a POST request body (for example) and you have this error, you probably just imported symfony/serializer
and forgot to import symfony/serializer-pack
对于后代,如果您将序列化程序注入控制器以反序列化 POST 请求正文(例如)并且出现此错误,您可能只是导入了
symfony/serializer
而忘记导入symfony/serializer-pack
more information on How to use the serializer有关如何使用序列化程序的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.