【问题标题】:Symfony2 FOSREST JMS\Serializer bundles errorSymfony2 FOSREST JMS\Serializer 捆绑错误
【发布时间】:2016-06-29 05:53:15
【问题描述】:

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

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

【问题讨论】:

  • 这太宽泛了。但我认为您必须添加配置文件的代码。哪个类与“fos_rest.serializer”相关
  • 我遵循了 symfony 教程的所有步骤,但总是出现同样的错误,在 appKernel.php 文件中我添加了新的 FOS\RestBundle\FOSRestBundle()、新的 JMS\SerializerBundle\JMSSerializerBundle()
  • 在 composer.json 我添加了 "friendsofsymfony/rest-bundle": "0.11.*", "jms/serializer-bundle": "0.12.x-dev" 但同样的错误再次
  • 请在答案中添加您的 cmets 的内容。您应该提供minimal reproducible example。
  • @GitaarLAB 你是对的,谢谢你的评论。

标签: php symfony fosrestbundle jmsserializerbundle


【解决方案1】:

FOSRestBundle 2.0 中引入了此异常(请参阅here),因为 2.0 中也引入了序列化器抽象层。 它通常由检测 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。

【讨论】:

猜你喜欢
  • 2017-11-09
  • 1970-01-01
  • 1970-01-01
  • 2015-03-17
  • 1970-01-01
  • 1970-01-01
  • 2014-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多