简体   繁体   English

JSON格式的Symfony Rest API自定义404错误页面

[英]Symfony rest API custom 404 error pages in JSON format

I don't get how to customize error pages, in a JSON format for a REST API, written with Symfony . 我不知道如何使用Symfony编写REST API的JSON格式的自定义错误页面。 I made this small demo : 我做了这个小演示

As you can see, the project is set up with the Twig Bundle and I have customized the 404 pages in JSON format.. 如您所见,该项目是使用Twig Bundle设置的,并且我已经以JSON格式自定义了404页。

I Have tested the API with the dev server. 我已经在开发服务器上测试了API。

The client received HTML 404 page and not JSON format (the header send with the curl command is supposed to set symfony to a response in JSON format). 客户端接收到HTML 404页面,而不是JSON格式(使用curl命令发送的标头应该将symfony设置为JSON格式的响应)。

You can do it by overriding the twig show error action by forcing it to render a JSON instead of HTML. 您可以通过强制显示XML而不是HTML来覆盖树枝显示错误操作来完成此操作。 I recommend you to see here: https://symfony.com/doc/4.2/reference/configuration/twig.html#exception-controller 我建议您在这里查看: https : //symfony.com/doc/4.2/reference/configuration/twig.html#exception-controller

Don't forget than Symfony can render .json.twig file 不要忘了Symfony可以渲染.json.twig文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM