我尝试通过官方文档处理所有的附加(Symfony 5):https://symfony.com/doc/current/controller/error_pages.html#overriding-error-output-for-non-html-formats
我希望将所有异常序列化为JSON格式。我在本主题中阅读了类似问题的解决方案:How to format all HttpExceptions as json in symfony5?,但没有解决我的问题。
这是我的文件: src/Controller/ShowController.php
<?php
namespace App\Controller;
use App\Component\ShowResponseHandlerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
class ShowController extends AbstractController
{
/**
* @Route("/show/{file}", name="show", requirements={"file"=".+"})
*/
public function index(string $file, string $storageDirectory): Response
{
$absolutePath = $storageDirectory.$file;
if(!file_exists($absolutePath)) {
throw new NotFoundHttpException("Resource not exists");
}
return new BinaryFileResponse($absolutePath);
}
}src/序列化程序/ProblemNormalizer.php
<?php
namespace App\Serializer;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class ProblemNormalizer implements NormalizerInterface
{
public function normalize($exception, string $format = null, array $context = [])
{
return [
'success' => false,
'exception'=> [
'message' => $exception->getMessage(),
'code' => $exception->getStatusCode(),
]
];
}
public function supportsNormalization($data, string $format = null)
{
return $data instanceof FlattenException;
}
}config/services.yaml
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
App\Serializer\ProblemNormalizer:
tags: ['serializer.normalizer']当然,我已经安装了symfony/序列化程序包
composer show -i | grep "symfony/serializer"
symfony/serializer v5.2.9 Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and JSON我做错了什么或者没有配置什么?它总是返回html输出。
更新
控制台输出:
symfony console debug:container | grep normalizer
Symfony\Component\Serializer\Normalizer\DenormalizerInterface alias for "serializer"
Symfony\Component\Serializer\Normalizer\ObjectNormalizer alias for "serializer.normalizer.object"
Symfony\Component\Serializer\Normalizer\PropertyNormalizer alias for "serializer.normalizer.property"
maker.auto_command.make_serializer_normalizer Symfony\Bundle\MakerBundle\Command\MakerCommand
maker.maker.make_serializer_normalizer Symfony\Bundle\MakerBundle\Maker\MakeSerializerNormalizer
serializer.denormalizer.array Symfony\Component\Serializer\Normalizer\ArrayDenormalizer
serializer.denormalizer.unwrapping Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer
serializer.normalizer.constraint_violation_list Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer
serializer.normalizer.data_uri Symfony\Component\Serializer\Normalizer\DataUriNormalizer
serializer.normalizer.dateinterval Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer
serializer.normalizer.datetime Symfony\Component\Serializer\Normalizer\DateTimeNormalizer
serializer.normalizer.datetimezone Symfony\Component\Serializer\Normalizer\DateTimeZoneNormalizer
serializer.normalizer.flatten_exception Symfony\Component\Messenger\Transport\Serialization\Normalizer\FlattenExceptionNormalizer
serializer.normalizer.form_error Symfony\Component\Serializer\Normalizer\FormErrorNormalizer
serializer.normalizer.json_serializable Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer
serializer.normalizer.mime_message Symfony\Component\Serializer\Normalizer\MimeMessageNormalizer
serializer.normalizer.object Symfony\Component\Serializer\Normalizer\ObjectNormalizer
serializer.normalizer.problem Symfony\Component\Serializer\Normalizer\ProblemNormalizer
serializer.normalizer.property Symfony\Component\Serializer\Normalizer\PropertyNormalizer
serializer.normalizer.uid Symfony\Component\Serializer\Normalizer\UidNormalizer
// To search for a specific service, re-run this command with a search term. (e.g. debug:container log) 发布于 2021-05-27 12:24:53
尝试从config/services.yaml中删除App\序列化程序\ProblemNormalizer。再次检查容器中是否有规格化器,symfony console debug:container | grep normalizer。通常,您应该在列表中看到您的规范化器而没有任何配置(Symfony通过NormalizerInterface识别它)。用curl http://localhost:8000/show/foo.doc -H "Accept: application/json"进行测试
https://stackoverflow.com/questions/67659809
复制相似问题