dooso0594 2016-01-31 21:02
浏览 24
已采纳

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

I am working on a REST API with FOS Rest Bundle and FOS User Bundle

The data being PUT to the server is:

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

The routing looks okay:

admin_user_put_user PUT ANY ANY /admin/users/{id}

It is reaching this code below:

/**
 * @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();

}

The problem is that it is not populating $userData with the data sent.

Could it be a configuration issue?

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 }

I understand the code is a little awkward and welcome suggestions for improvement.

The absence of the group data in the save code is deliberate, I'll get to that later.

  • 写回答

1条回答 默认 最新

  • dongshan1396 2016-01-31 21:58
    关注

    Check the existence of SensioFrameworkExtraBundle and if it's configured as shown here (sensio_framework_extra and fos_rest).

    Also you may try to define a class attribute, so it would map without a body converter (to see if it works):

    @ParamConverter("userData", class="AppBundle:User")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大