使用symfony3进行API Rest:序列化yaml文件不起作用

时间:2017-01-13 00:16:43

标签: json serialization symfony fosrestbundle jmsserializerbundle

我有一个带有FOSRestBundle和JMSSerializerBundle的symfony 3项目安装了这个配置:

fos_rest:
    routing_loader:
        default_format: json
    format_listener: false
    param_fetcher_listener: true
    view:
        view_response_listener: true

jms_serializer:
    metadata:
        auto_detection: true

    visitors:
        json:
            options: 0 # json_encode options bitmask

    handlers:
        datetime:
            default_format: "c"
            default_timezone: "UTC"

我有一个实体插入:

{# src/MyBundle/Entity/Insertion.php #}

namespace MyBundle\Entity

class Insertion
{   
    private $id;
    private $name;
    private $date;


    // With getter and setter
}

我为序列化创建了一个配置文件

{# src/MyBundle/Resources/config/serializer/Entity.Insertion.yml #}

MyBundle\Entity\Insertion:
    exclusion_policy: ALL
    properties:
        id:
            expose: false
        name:
            expose: true

最后我创建了API控制器:

{# src/MyBundle/Controller/API/InsertionController.php #}

namespace MyBundle\Controller\API;

use FOS\RestBundle\Controller\FOSRestController;

class InsertionController extends FOSRestController 
{
    /**
    * Return all insertions
    *
    * // Some Params
    *
    * @param ParamFetcher $paramFetcher
    * @return \Symfony\Component\HttpFoundation\Response
    */
    public function getInsertionsAction(ParamFetcher $paramFetcher)
    {
        /** @var InsertionRepository $insertionsRepository */
        $insertionsRepository = $this->getDoctrine()->getRepository("MyBundle:Insertion");

        $insertions = $insertionsRepository->findByParameters($paramFetcher);
        $view = $this->view($insertions, 200);
        return $this->handleView($view);
    }
}

当我调用api时,我会公开所有插入的属性(yaml文件不起作用)。 Insertion类还有许多相关的类,它们不适用于JSON。我尝试了所有配置,但我仍然无法使串行器yaml文件正常工作......

如何解决?

0 个答案:

没有答案