2016-03-14 29 views
-1

你能幫忙解決下面的錯誤?Symfony2的FOSREST JMS 串行束錯誤

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 
+0

這是過於寬泛。但我認爲你必須添加你的配置文件的代碼。哪個類與「fos_rest.serializer」相關 –

+0

我遵循symfony教程的所有步驟,但始終是相同的錯誤,在appKernel.php文件中我添加了新的FOS \ RestBundle \ FOSRestBundle(), 新的JMS \ SerializerBundle \ JMSSerializerBundle() – Ndb

+0

在composer.json我補充說:「friendsofsymfony /休息束」:「0.11 *」, 「JMS /串行束」:「0.12.x-dev的」,但同樣的錯誤再次 – Ndb

回答

3

此異常已在FOSRestBundle 2.0被引入(參見here),因爲在2.0中引入過串行抽象層。 它通常由檢測JMS串行器的bundle自動管理。

你加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

+1

你是怎麼做到的?你能否在你的回答中解釋一下? –

+1

對不起,我的意思是這可能是什麼讓這個錯誤發生,我會改進我的答案。 –

+1

@ A.L更新:-) –