Symfony2 FOSREST JMS \ Serializer捆绑错误

时间:2016-03-14 09:09:42

标签: php symfony fosrestbundle jmsserializerbundle

您能帮忙解决以下错误吗?

InvalidArgumentException: "fos_rest.serializer" must implement FOS\RestBundle\Serializer\Serializer (instance of "JMS\Serializer\Serializer" given).
in C:\wamp\www\SymfonyRestAPI\src\FOS\RestBundle\DependencyInjection\Compiler\SerializerConfigurationPass.php line 58
at SerializerConfigurationPass->process(object(ContainerBuilder)) in C:\wamp\www\SymfonyRestAPI\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\Compiler.php line 117
at Compiler->compile(object(ContainerBuilder)) in C:\wamp\www\SymfonyRestAPI\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\ContainerBuilder.php line 613
at ContainerBuilder->compile() in C:\wamp\www\SymfonyRestAPI\app\bootstrap.php.cache line 2502
at Kernel->initializeContainer() in C:\wamp\www\SymfonyRestAPI\app\bootstrap.php.cache line 2281
at Kernel->boot() in C:\wamp\www\SymfonyRestAPI\app\bootstrap.php.cache line 2312
at Kernel->handle(object(Request)) in C:\wamp\www\SymfonyRestAPI\web\app_dev.php line 28

1 个答案:

答案 0 :(得分:4)

由于2.0中引入了串行器抽象层,因此FOSRestBundle 2.0中引入了此异常(请参阅here)。 它通常由检测JMS序列化程序的包自动管理。

您是否将JMSSerializerBundle添加到内核中?

// app/AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new JMS\SerializerBundle\JMSSerializerBundle(),,
        );

        // ...
    }
}

这应该是问题所在。

如果不是,那么您可能尝试直接定义FOSRestBundle使用的序列化程序,以下在大多数情况下都是不好的做法,只有在您知道自己做了什么时才应该使用

// services.yml
services:
    fos_rest.serializer:
        // This won't work in case your class doesn't
        // implement FOS\RestBundle\Serializer\Serializer
        class: My\Serializer 

如果我的第二个命题是你的情况,你自定义序列化程序必须实现接口FOS\RestBundle\Serializer\Serializer