我正在我的项目中实现FOSUserBundle。我需要一个基本的GET操作来返回ContactList实体的json对象。
主计长:
class ContactListController extends FOSRestController
{
use ViewContextTrait;
const DEFAULT_GROUPS = ['organization_list'];
/**
* @ParamConverter("contactList", class="SchemaBundle:ContactList")
* @param ContactList $contactList
* @return Response
*/
public function getAction(ContactList $contactList)
{
return $this->handleView($this->viewWithContext($contactList, Response::HTTP_OK));
}特点:
use FOS\RestBundle\Context\Context;
use FOS\RestBundle\View\View;
trait ViewContextTrait
{
public function viewWithContext($data, $statusCode = null, $groups = self::DEFAULT_GROUPS)
{
$context = new Context();
$context->setGroups($groups);
return View::create($data, $statusCode)->setContext($context);
}
}我的config.yml
fos_rest:
routing_loader:
include_format: false
body_listener:
array_normalizer: fos_rest.normalizer.camel_keys
param_fetcher_listener: true
view:
view_response_listener: 'force'
format_listener:
rules:
- { path: '^/api', priorities: ['json'], fallback_format: json, prefer_extension: false }问题:当我通过postman (/api/contact-list/1)调用这个路由时,我总是在Response对象中为我的内容获得一个{}。
这是被抛出的回应:

,为了返回序列化的ContactList实体w/响应中的上下文组,我遗漏了什么?
发布于 2018-10-02 20:07:46
解决方案:首先,我的注释不包括在内。我不得不把这个加到我的config.yml里
framework:
serializer:
enabled: true
enable_annotations: true接下来,我忘记了我之前在我的项目中包含了JMS序列化程序。显然,它所使用的ViewHandler用于Rest Bundle具有序列化程序服务的默认顺序。。最后,我不得不把这个包含在我的config.yml中
fos_rest:
service:
serializer: fos_rest.serializer.symfony默认情况下,FOSRest使用的是未正确配置的JMS序列化程序。
https://stackoverflow.com/questions/52611559
复制相似问题