使用QueryParam或ParamConverter的FOSRestBundle不起作用

时间:2014-05-20 10:47:31

标签: symfony fosrestbundle jmsserializerbundle

我遇到了FOSRestBundle的问题。一切都运行正常,直到我添加@QueryParam注释(在我的cgetAction方法上)和@ParamConverter注释(在我的getAction方法上)。我不知道会出现什么问题,但我得到的错误就是这个......

The format "" is not supported for deserialization. 415 Unsupported Media Type - UnsupportedMediaTypeHttpException

可以这么说的失败点在于:

vendor\friendsofsymfony\rest-bundle\FOS\RestBundle\Request\AbstractRequestBodyParamConverter.php at line 118

我对特定控制器的注释如下(我只是发布cgetAction,因为我确定它们是以某种方式相关的,我只是不知道如何)

/**
 * Lists all School entities.
 *
 * @QueryParam(name="page", requirements="\d+", default="1", description="Current page of school listing")
 * @View(template="LMSSchoolBundle:School:index.html.twig", templateVar="pagination")
 */
public function cgetAction(ParamFetcher $fetcher)
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('LMSSchoolBundle:School')->createQueryBuilder('s');

    $pagination = $this->get('knp_paginator')->paginate(
        $entities,
        $fetcher->get('page'),
        $this->container->getParameter('page_item_limit')
    );

    $groups = ($this->get('security.context')->isGranted('ROLE_API')) ? 'restapi' : 'standard';

    $view = $this->view($pagination)
                 ->getSerializationContext()
                 ->setGroups($groups);

    return $view;
}

最后,我的config.yml的fos_rest部分:

fos_rest:
    routing_loader:
        include_format: false
    body_converter:
        enabled: true
        validate: true
        validation_errors_argument: validationErrors
    param_fetcher_listener: true
    body_listener:
        decoders:
            json: fos_rest.decoder.jsontoform
            xml: fos_rest.decoder.xml
    allowed_methods_listener: true
    access_denied_listener:
        json: true
    disable_csrf_role: ROLE_API
    view:
        formats:
            json: true
            rss: false
            xml: true
        templating_formats:
            html: true
        force_redirects:
            html: true
        failed_validation: HTTP_BAD_REQUEST
        default_engine: twig
        view_response_listener: 'force'
    format_listener:
        rules:
            - { path: '^/css', priorities: [ 'css' ], fallback_format: false, prefer_extension: true }
            - { path: '^/js', priorities: [ 'js' ], fallback_format: false, prefer_extension: true }
            - { path: '^/', priorities: [ 'html', 'json', 'xml' ], fallback_format: html, prefer_extension: true }
            - { path: '^/', host: 'api.%domain%', priorities: [ 'json', 'xml' ], fallback_format: json, prefer_extension: false }

注意 - ^/css^/js规则在那里,因为捆绑包弄乱了我的布局等等,不知道这是否是正确的方法呢?我真的不知道整个配置部分是否正确!文档非常复杂,恕我直言。

无论如何,如果我删除@QueryParam注释,它会继续进入函数而不会出现问题。但是如果我添加它,应用程序会在它到达$em = $this->...

之前退出

感谢您的帮助!

编辑哦顺便说一句,我已经想到这可能与JMSSerializer有关,但网站似乎目前正在下降(至少在南非),所以是啊......

0 个答案:

没有答案