序列化数据库中的数据

Serialize data from database

我的 Symfony rest 有问题 API。我有这个控制器:

<?php

namespace App\Controller;

use App\Entity\Post;
use App\Service\ErrorDecoratorService;
use App\Service\PostService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Serializer\SerializerInterface;

class PostController extends AbstractController
{
    private $postService;
    private $errorDecoratorService;

    public function __construct(PostService $postService, ErrorDecoratorService $errorDecoratorService)
    {
        $this->postService = $postService;
        $this->errorDecoratorService = $errorDecoratorService;
    }


    /**
     * @Route("/post/{postId}", methods={"GET"})
     * @param $postId
     * @param SerializerInterface $serializer
     * @return JsonResponse
     */
    public function getPost($postId, SerializerInterface $serializer)
    {
        $result = $this->postService->getPost($postId);
        if ($result instanceof Post) {

            $data = $serializer->serialize($result,'json');
            $status = JsonResponse::HTTP_OK;
        } else {
            $status = JsonResponse::HTTP_NOT_FOUND;
            $data = $this->errorDecoratorService->decorateError($status, $result);
        }
        return new JsonResponse($data, $status);
    }

}

当我从数据库序列化为 JSON 格式时,我收到此错误:

Could not normalize object of type App\Entity\Post, no supporting normalizer found. (500 Internal Server Error)

这是我的实体

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

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

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

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

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

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

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getContent(): ?string
    {
        return $this->content;
    }

    public function setContent(string $content): self
    {
        $this->content = $content;

        return $this;
    }

    public function getAuthor(): ?string
    {
        return $this->author;
    }

    public function setAuthor(string $author): self
    {
        $this->author = $author;

        return $this;
    }
}

我的错误在哪里?我已经尝试过 Symfony Serializer、JMS Serializer,但我仍然遇到同样的错误。

一旦你 序列化程序将生成一个 json 编码的 字符串 与此行:

$data = $serializer->serialize($result,'json'); 
// "{\"id\":1,\"title\":\"qwerty\",\"content\":\"Lorem ipsum sit dolor amet\",\"author\":\"Pawel\"}"

JsonResponse 构造 json 对第一个参数进行编码,通常是一个数组或对象,但在您的情况下只是一个 字符串 。您需要在将字符串传递给构造函数时对其进行解码,或者最好使用 JsonResponse::fromJsonString() 方法。

应该有效:

return new JsonResponse(json_decode($data), $status);

首选方法:

return new JsonResponse::fromJsonString($data, $status);

https://symfony.com/doc/current/components/http_foundation.html#creating-a-json-response