我正在尝试使用FOSRestBundle,JMSSerializerBundle库检索json格式的用户列表,但是有问题。 控制器看起来像:
namespace System\ApiBundle\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations\View;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class UserController extends Controller
{
/**
* @View()
* @return array
*/
public function allAction()
{
$em = $this->getDoctrine()
->getEntityManager();
$users = $em->getRepository('AppBundle:User')
->findAll();
return array(
'users' => $users,
);
}
..........
但它会返回此错误
{
"error":
{
"code": 500,
"message": "Internal Server Error",
"exception":
[
{
"message": "Warning: Invalid argument supplied for foreach()",
"class": "Symfony\\Component\\Debug\\Exception\\ContextErrorException",
"trace":
[
{
"namespace": "",
"short_class": "",
"class": "",
"type": "",
"function": "",
"file": "/var/www/html/project/vendor/jms/serializer/src/JMS/Serializer/GenericSerializationVisitor.php",
"line": 101,
"args":
[
]
},
..................
但是如果我用find($ id)替换findAll(),则特定用户的json会出现没有任何问题。