【发布时间】:2015-07-18 19:09:43
【问题描述】:
我已将 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-Type 设置为 application/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.yml 和src/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 这样的工具并设置正确的标头,我会得到我想要的实体,见下图:
我找不到问题出在哪里,所以我在这里非常需要有人的帮助,因为我已经没有想法了
【问题讨论】:
-
您是否将 'Content-Type' 设置为 'application/json' ?
-
@Jeet 我应该在哪里做?是不是像您在 OP 上看到的那样处于 FOSRes 的配置?
-
在 Google Chrome 的 Advanced Rest Client 应用程序(应用程序扩展)中尝试 app_dev.php/api/v1/reps,您将有一个选择。
-
@Jeet 看到 OP,我已经编辑添加了一些信息
-
FOSRestBundle负责为其发送给客户端的响应设置标头。但其客户端(移动应用程序、REST 客户端扩展)有责任在将其发送到服务器时设置正确的标头值。FOSRestBundle可以做出相应的回应。 ;)
标签: php json symfony fosrestbundle symfony-2.6