PUT 数据通过 fos_rest.request_body ParamConverter - Symfony 3.


PUT data through fos_rest.request_body ParamConverter - Symfony 3

我正在开发带有FOS Rest Bundle和FOS User Bundle的REST API

PUT 到服务器的数据是:

{"id":1,"email":"email@example.com","用户名":"adminuser","enabled":true,"locked":false,"groups" :[]}

路由看起来正常:

admin_user_put_user放置任何/admin/users/{id}

它正在达到下面的代码:

/**
 * @ParamConverter("userData", converter="fos_rest.request_body")
 * @View(statusCode=204)
 */
public function putUserAction( $id, User $userData )
{
    $this->denyAccessUnlessGranted( 'ROLE_ADMIN', null, 'Unable to access this page!' );
    $em = $this->getDoctrine()->getManager();
    $user = $em->getRepository( 'AppBundle:User' )->find( $id );
    $user->setEmail( $userData->getEmail() );
    $user->setEmailCanonical( $userData->getEmailCanonical() );
    $user->setEnabled( $userData->isEnabled() );
    $user->setLocked( $userData->isLocked() );
    $em->flush();
}

问题是它没有用发送的数据填充$userData

可能是配置问题吗?

fos_rest:
    disable_csrf_role: ROLE_API
    view:
        view_response_listener: true
        force_redirects:
            html: true
        formats:
            json: true
        templating_formats:
            json: false
            html: true
        mime_types:
            json: ['application/json', 'application/x-json']
        failed_validation: HTTP_BAD_REQUEST
    body_listener: 
        default_format: json
    param_fetcher_listener: 
        force: true
    allowed_methods_listener: true
    access_denied_listener:
        json: true
    body_converter:
        enabled: true
        # validate: true
        # validation_errors_argument: validationErrors # This is the default value
    exception:
        enabled: true
    routing_loader:
        default_format: json
        include_format: false
    format_listener:
        rules:
            -
                # http://symfony.com/doc/current/bundles/FOSRestBundle/format_listener.html
                path: ^/admin/users
                priorities: ['json', 'html', 'xml','form']
                fallback_format: ~
                prefer_extension: true
                methods: [ 'get', 'post', 'put', 'delete']
            - { path: '^/', stop: true }

我知道代码有点尴尬,欢迎提出改进建议。

保存代码中缺少组数据是故意的,我稍后会谈到这一点。

检查是否存在 SensioFrameworkExtraBundle,以及它是否按此处所示(sensio_framework_extrafos_rest)进行配置。

您也可以尝试定义一个类属性,这样它就可以在没有主体转换器的情况下进行映射(看看它是否有效):

@ParamConverter("userData", class="AppBundle:User")