我已按照以下方法配置了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
这样的工具,并设置适当的头信息,我可以得到我想要的实体,如下图所示:
我找不到问题出在哪里,所以我非常需要有人帮助,因为我已经没有任何想法了。
$entities
无法正常转化为JSON,我之前也遇到过同样的问题。试着发送一个简单的数组,看看JSON数据是否正确返回。 - Jeet