13

我已将 FOSRestBundle 配置如下:

#FOSRestBundle
fos_rest:
    param_fetcher_listener: true
    body_listener: true
    format_listener:
        rules:
            - { path: ^/, priorities: [ json, html ], fallback_format: ~, prefer_extension: true }
        media_type:
            version_regex: '/(v|version)=(?P<version>[0-9\.]+)/'

    body_converter:
        enabled: true
        validate: true

    view:
        mime_types:
            json: ['application/json', 'application/json;version=1.0', 'application/json;version=1.1']
        view_response_listener: 'force'
        formats:
            xml:  false
            json: true
        templating_formats:
            html: true

    exception:
        codes:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
            'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
        messages:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
    allowed_methods_listener: true
    access_denied_listener:
        json: true

我在控制器上有这个:

namespace PDI\PDOneBundle\Controller\Rest;

use FOS\RestBundle\Controller\FOSRestController;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Controller\Annotations\Get;

class RepresentativeRestController extends FOSRestController
{
    /**
     * Get all representatives.
     *
     * @return array
     *
     * @ApiDoc(
     *   resource = true,
     *       https = true,
     *   description = "Get all representatives.",
     *   statusCodes = {
     *      200 = "Returned when successful",
     *      400 = "Returned when errors"
     *   }
     * )
     * @Get("/api/v1/reps")
     */
    public function getRepsAction()
    {
        $em = $this->getDoctrine()->getManager();
        $entities = $em->getRepository('PDOneBundle:Representative')->findAll();

        if(!$entities)
        {
            return $this->view(null, 400);
        }

        return $this->view($entities, 200);
    }
}

但是当我尝试以下 URL 时,app_dev.php/api/v1/reps我得到了这个错误:

找不到模板“”。500 内部服务器错误 - InvalidArgumentException 3 个链接异常:Twig_Error_Loader » InvalidArgumentException » InvalidArgumentException »

我希望 API 返回格式良好的 JSON,如下例所示:

{
   "id":"30000001",
   "veeva_rep_id":"0055648764067SwzAAE",
   "display_name":"John Know",
   "avatar_url":"http://freelanceme.net/Images/default%20profile%20picture.png",
   "rep_type":"VEEVA",
   "username":"john@mail.com",
   "first":"John",
   "last":"Know",
   "title":"Sales Representative",
   "phone":"800-555-1212",
   "email":"john@mail.com",
   "territory_id":"200454001",
   "inactive":"no",
   "total_contacts":"6",
   "total_shares":"0",
   "totalViews":"0",
   "lastLoginAt":"2015-05-05 15:45:57",
   "lastVeevaSyncAt":"2015-05-05 15:45:57",
   "createdAt":"2015-05-05 15:45:57",
   "updatedAt":"2015-05-05 15:45:57"
}

FOSRestBundle 是否配置为返回 JSON?为什么还要求 Twig 模板?我怎样才能解决这个问题?

第一次测试:

正如@Jeet 建议我的那样,我尝试过使用 Postman(与他告诉我的扩展名相同)并且在将标题设置Content-Typeapplication/json错误之后变成了这个

格式错误的 JSON

所以,FOSRestBundle 没有按照应有的方式设置标头,并且控制器没有返回有效的 JSON,我该如何解决这些问题?

第二次测试:

正如@Jeet所建议的,我运行了这个测试:

/**
 * Get all representatives.
 *
 * @return array
 *
 * @ApiDoc(
 *   resource = true,
 *       https = true,
 *   description = "Get all representatives.",
 *   statusCodes = {
 *      200 = "Returned when successful",
 *      400 = "Returned when errors"
 *   }
 * )
 * @Get("/api/v1/reps")
 * @View()
 */
public function getRepsAction()
{
    $em = $this->getDoctrine()->getManager();
    $entities = $em->getRepository('PDOneBundle:Representative')->findAll();

    $temp = array("1", "2", "3");

    $view = $this->view($temp, Codes::HTTP_OK);
    return $this->handleView($view);
}

仍然是同样的问题:

找不到模板“”。500 内部服务器错误 - InvalidArgumentException 3 个链接异常:Twig_Error_Loader » InvalidArgumentException » InvalidArgumentException »

这里还有什么问题?我在配置时遗漏了什么吗?

我忘了添加app/config/routing.ymlsrc/PDI/PDOneBundle/Resources/config/routing.yml一开始他们就在这里,也许这是拼图上缺少的部分,让您更好地了解问题出在哪里:

#app/config/routing.yml
#PDOne
pdone:
    resource: "@PDOneBundle/Resources/config/routing.yml"

template:
    resource: "@TemplateBundle/Resources/config/routing.yml"

#FOSUserBundle
fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"
    prefix: /

#NelmioApiDocBundle:
NelmioApiDocBundle:
    resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
    prefix:   /api/doc

#SonataAdmin
admin:
    resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
    prefix: /admin

_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /admin

#src/PDI/PDOneBundle/Resources/config/routing.yml
pdone:
    resource: "@PDOneBundle/Controller/"
    type:     annotation
    prefix:   /

第三次测试:

来自客户端的请求肯定有问题,如果我使用类似工具Postman并设置正确的标头,我会得到我想要的实体,见下图:

在此处输入图像描述

我找不到问题出在哪里,所以我在这里非常需要有人的帮助,因为我已经没有想法了

4

4 回答 4

26

正如人们所建议的那样:只有 Accept 标头或扩展才能为您提供 JSON。似乎您已经使用 Accept 标头进行了排序。

为了使用扩展,你必须告诉你想如何在 Symfony 中设置格式。

此代码应该为您提供所需的输出:

namespace RestTestBundle\Controller;

use FOS\RestBundle\Controller\Annotations\View;

use FOS\RestBundle\Controller\Annotations\Get;

class YourController
{
    /**
     * @Get("/api/v1/reps.{_format}", defaults={"_format"="json"})
     * @View()
     */
    public function indexAction()
    {
        return array(
            'status' => 'ok',
            'companies' => array(
                array('id' => 5),
                array('id' => 7),
            ),
        );
    }
}

Edit1:如果您不想使用View类,而是使用纯数组:不要忘记禁止 SensioExtraBundle 的视图处理

sensio_framework_extra:
    view:    { annotations: false }

Edit2:如果您不使用 HTML 格式并且只想有一个 json 输出,您可以使用这样的配置:

fos_rest:
    # ....
    format_listener:
        rules:
            - { path: ^/, priorities: [ json ], fallback_format: json, prefer_extension: true }
    # ....

解释为什么您会看到错误“未找到视图”:

TL;DR:您的浏览器发送一个 Accept 标头,告诉 FOSRestBundle 输出一个“html”变体。

背景:此捆绑包主要与 Accept 标头一起使用,最好使用所有可能的输出格式:html(您可以使用您提供的表单、对象列表、对象的详细信息通过这种方式轻松测试您的 REST API)、json ,xml。有时甚至是图像 mime 类型,例如 image/jpeg、image/png 作为默认值或 json/xml 作为变体(您可以使用 base64 图像表示)。

说明:如果您打开浏览器的“网络”选项卡并查看它发送的标题,您会注意到类似:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8这意味着“按此顺序使用”:

  1. 文本/html
  2. 应用程序/xhtml+xml
  3. application/xml优先级为0.9 ,根据您的配置禁止使用
  4. */* 优先级为0.8,表示任何格式

如果您仔细观察,您会看到根据您的配置text/html是您的配置具有的变体之一('html'),而*/*是另一个变体('json'),但text/html有一个优先级为 1,而*/*的优先级为 0.8,因此text/html匹配,FOSRestBundle 尝试查找 HTML 表示并失败。

PS:如果您多次发布问题 - 请确保您注意每个线程中的所有回复。

于 2015-05-12T14:35:46.467 回答
2

您可以通过两种方式做出回应

return View::create($entities, Codes::HTTP_OK);

或者

$view = $this->view($entities, Codes::HTTP_OK);    
return $this->handleView($view)
于 2015-05-08T07:32:53.017 回答
2

你可以简单地使用

            $view = new View($form);
            $view->setFormat('json');
            return $this->handleView($view);
于 2018-07-25T11:07:27.117 回答
1

FosRestBundle 利用 Accept Header。这意味着它会根据您的请求返回响应。通过访问“app_dev.php/api/v1/reps”路由,您隐含地请求 html 格式,因此它会尝试提供模板。

app_dev.php/api/v1/reps.json 是否返回您需要的内容?

您还应该测试 app_dev.php/api/v1/reps.xml 并期待 xml 输出

于 2015-05-08T07:08:21.710 回答