Symfony 序列化程序问题 - NotNormalizableValueException [英] Symfony Serializer issue - NotNormalizableValueException

查看:33
本文介绍了Symfony 序列化程序问题 - NotNormalizableValueException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 Symfony 4.1 中使用带有 FOSRestBundle 的序列化程序时遇到问题

I have an issue when I'm using the serializer with FOSRestBundle in Symfony 4.1

我收到以下错误消息:

无法规范化 App\Entity\Youp 类型的对象,找不到支持的规范化器.Symfony\Component\Serializer\Exception\NotNormalizableValueException

Could not normalize object of type App\Entity\Youp, no supporting normalizer found. Symfony\Component\Serializer\Exception\NotNormalizableValueException

我不明白为什么我会遇到这个问题,Symfony 的序列化器应该有一个序列化器对象,否则我错过了什么?

I don't understand why I have this issue, Symfony's Serializer should have an serializer object or I miss something ?

见下面我的控制器和我的实体:

See bellow my controller and my entity :

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\YoupRepository")
 */
class Youp
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }
}

<小时>

<?php 

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\View\View;
use FOS\RestBundle\Controller\Annotations as Rest;

use App\Entity\Youp;

class BidonController extends FOSRestController {

  /**
   * @Rest\View()
   * @Rest\Get("/youps")
   */
  public function getPharmacies() {
    $youps = $this->getDoctrine()->getRepository(Youp::class)->findAll();
    return $youps; 
  }
}

推荐答案

您的对象的属性是private,因此序列化程序不知道如何规范化或从您的对象获取任何数据.您可以将属性设置为 public 或启用 ObjectNormalizer(使用 PropertyAccess 组件访问私有/受保护的属性)和/或 GetSetMethodNormalizer>(通过调用getter"读取类的内容)在您的配置中使用以下服务定义:

Your object's properties are private so the serializer doesn't know how to normalize or get any data from your object. You can either set the properties to public or enable the ObjectNormalizer (which uses the PropertyAccess Component to access the private/protected properties) and/or GetSetMethodNormalizer (which reads the content of the class by calling the "getters") using the following service-definition in your configuration:

services:
  # [..]
  Symfony\Component\Serializer\Normalizer\ObjectNormalizer:
    class: Symfony\Component\Serializer\Normalizer\ObjectNormalizer
    public: false
    tags:
      - { name: 'serializer.normalizer' }

  Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer:
    class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer
    public: false
    tags:
      - { name: 'serializer.normalizer' }

之后清除缓存.可以在文档 中找到有关已包含在序列化程序组件中的规范化程序的更多信息一个>

Clear your cache afterwards. More information about the normalizers already included in the serializer component can be found in the documentation

这篇关于Symfony 序列化程序问题 - NotNormalizableValueException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆