简体   繁体   English

无法对类型的对象进行非规范化,找不到支持的规范化器。 Symfony 2.8

[英]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到目前为止我尝试过的事情

  • Serialize an object to json/xml and tried to deserialize it again将对象序列化为 json/xml 并尝试再次反序列化
  • Decode json/xml to an array and then denormalize to an object(decode is successful but denormalizing gives the same error)将 json/xml 解码为数组,然后反规范化为对象(解码成功但反规范化给出相同的错误)
  • Tried to use serializer as a service as described in Here尝试作为中的描述使用串行作为一项服务在这里

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.

相关问题 Symfony Serializer不会反序列化为不同命名空间中的对象:&#39;找不到支持的规范化器&#39; - Symfony Serializer doesn't deserialize into objects in different namespace: 'no supporting normalizer found' 带有警卫的 Symfony 身份验证总是返回“找不到用户名”。 - Symfony authentication with guard always return “Username could not be found.” 找不到CakePHP JsController。 - CakePHP JsController could not be found. Symfony2.8 -&gt; 3.3 Sonata\\CoreBundle\\Form\\Extension\\DependencyInjectionExtension::__construct() 必须是数组类型,给定对象,调用 - Symfony2.8 -> 3.3 Sonata\CoreBundle\Form\Extension\DependencyInjectionExtension::__construct() must be of the type array, object given, called in 为什么Symfony 2.8服务中的请求对象为空? - why request object in Symfony 2.8 service is empty? Symfony规范化器和ArrayCollections - Symfony Normalizer and ArrayCollections 找不到使用Symfony 3 addIndex进行的教义迁移。 为什么? - Doctrine Migrations with Symfony 3 addIndex not found. Why? PHP Normalizer:致命错误:未找到类“ Normalizer” - PHP Normalizer: Fatal error: Class 'Normalizer' not found 找不到Laravel服务提供商。 为什么? - Laravel service provider could not be found. why? 找不到驱动程序pdo_pgsql symfony 2.8 - Could not find driver pdo_pgsql symfony 2.8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM