简体   繁体   English

FOSRestBundle返回空内容

[英]FOSRestBundle returning empty content

I'm implementing FOSUserBundle in my project. 我在我的项目中实现FOSUserBundle。 I want a basic GET action to return a json object of the ContactList entity. 我想要一个基本的GET操作来返回ContactList实体的json对象。

Controller: 控制器:

class ContactListController extends FOSRestController
{
    use ViewContextTrait;
    const DEFAULT_GROUPS = ['organization_list'];
    /**
     * @ParamConverter("contactList", class="SchemaBundle:ContactList")
     * @param ContactList $contactList
     * @return Response
     */
    public function getAction(ContactList $contactList)
    {
        return $this->handleView($this->viewWithContext($contactList, Response::HTTP_OK));
    }

Trait: 特征:

use FOS\RestBundle\Context\Context;
use FOS\RestBundle\View\View;
trait ViewContextTrait
{
    public function viewWithContext($data, $statusCode = null, $groups = self::DEFAULT_GROUPS)
    {
        $context = new Context();
        $context->setGroups($groups);
        return View::create($data, $statusCode)->setContext($context);
    }
}

My config.yml : 我的config.yml

fos_rest:
    routing_loader:
        include_format: false
    body_listener:
        array_normalizer: fos_rest.normalizer.camel_keys
    param_fetcher_listener: true
    view:
        view_response_listener: 'force'
    format_listener:
        rules:
            - { path: '^/api', priorities: ['json'], fallback_format: json, prefer_extension: false }

The problem: When I call this route via postman ( /api/contact-list/1 ), I always get a {} for my content in the Response object. 问题:当我通过邮递员( /api/contact-list/1 )调用此路由时,我总是在Response对象中得到一个{}作为我的内容。

This is the dumped response: 这是转储的响应:

What am I missing in order to return the serialized ContactList entity w/ the context group in my Response? 为了在响应中返回带有上下文组的序列化ContactList实体,我缺少什么?

Solution: First of all, my annotations weren't included. 解决方案:首先,不包含我的注释。 I had to add this to my config.yml : 我必须将其添加到我的config.yml

framework:
    serializer:
      enabled: true
      enable_annotations: true

Next, I forgot I had previously included JMS serializer into my project awhile back. 接下来,我忘记了之前曾经将JMS序列化程序包含在我的项目中。 Apparently the ViewHandler for the Rest Bundle has a default order of serializer services that it uses. 显然, Rest Bundle的ViewHandler 具有使用的默认序列化程序服务顺序 I ended up having to include this in my config.yml : 我最终不得不在config.yml

fos_rest:
    service:
        serializer: fos_rest.serializer.symfony

By default, FOSRest was using JMS Serializer which wasn't configured properly. 默认情况下,FOSRest使用的JMS序列化器配置不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM