[英]problem with converting a entity to json using jms_serializer - Symfony 4
我正在尝试制作一个 api 以使用 symfony 4 返回一个 json 我创建了一个实体并且它工作正常但它不会将数据库的数据转换为 json 所以来自“Symfony\\Component\\Serializer\\Serializer”的序列化程序给我错误
serialization for the format json is not supported
所以我尝试了 Jms_Serializer 但在官方网站上他们使用旧版本的 symfony 我安装了捆绑包
composer require jms/serializer-bundle
这是控制器中的代码
class ProduitsController extends AbstractController
{
/**
* @Route("/api/produits/cuisine")
*/
public function index()
{
$dc = $this->getDoctrine();
$Produits=$dc->getRepository(Article::class)->findAll();
$data= $this->get('jms_serializer')->serialize($Produits,'json');
return new JsonResponse($data);
}
}
我收到此错误: ServiceNotFoundException
Service "jms_serializer" not found: even though it exists in the app's
container, the container inside "App\Controller\ProduitsController" is a
smaller service locator that only knows about the "doctrine", "form.factory",
"http_kernel", "parameter_bag", "request_stack", "router",
"security.authorization_checker", "security.csrf.token_manager",
"security.token_storage", "serializer", "session" and "twig" services. Try
using dependency injection instead
一个简单的 json_encode() 结果给我一个空的 json
没关系,它工作正常问题出在 AbstractController 类上,我将其更改为 Controller
class ProduitsController extends Controller
{
/**
* @Route("/api/produits/cuisine")
*/
public function index()
{
$dc = $this->getDoctrine();
$Produits=$dc->getRepository(Article::class)->findAll();
$data= $this->get('jms_serializer')->serialize($Produits,'json');
return new JsonResponse($data);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.