使用Play框架处理REST URL错误

时间:2021-02-28 11:24:05

Currently when I (or more importantly, a user) type in one of my rest functions into the URL, it works, with the 200 status code. But if you type a wrong one or mispell it, a 404 page is generated, with a 404 status code when looking at it through a REST client.

目前,当我(或更重要的是,用户)在我的其余功能中键入URL时,它可以使用200状态代码。但是如果您键入错误或错误拼写,则会生成404页面,并在通过REST客户端查看时显示404状态代码。

Instead of getting a 404 page when the bad URL is sent, I would instead like to display a dynamically generated JSON object.

我不想在发送错误的URL时获取404页面,而是希望显示动态生成的JSON对象。

How do I fix that error handling to do what I want, Is there a place where I can define what should be done during a particular status code?

如何修复错误处理以执行我想要的操作,是否有一个地方可以定义在特定状态代码期间应该执行的操作?

1 个解决方案

#1


7  

I am not very familiar with the Play Framework, but I was interested. This discussion seemed at least similar to what you want:

我对Play Framework不太熟悉,但我很感兴趣。这个讨论似乎至少与你想要的相似:

Gaëtan Renaudeau

GaëtanRenaudeau

...

...

You can customize errors pages depending of the http code error (404, 500, 403, ...) by editing app/views/errors/{code}.html files where {code} is you http code. If you are using other format than html (like xml, json) you can have 404.json , 404.xml, etc...

您可以通过编辑app / views / errors / {code} .html文件来自定义错误页面,具体取决于http代码错误(404,500,403,...),其中{code}是您的http代码。如果你使用的是其他格式而不是html(如xml,json),你可以拥有404.json,404.xml等...

So, modify:

所以,修改:

app/views/errors/404.type_of_response

Hopefully this at least points you in the right direction.

希望这至少可以指出你正确的方向。

#1


7  

I am not very familiar with the Play Framework, but I was interested. This discussion seemed at least similar to what you want:

我对Play Framework不太熟悉,但我很感兴趣。这个讨论似乎至少与你想要的相似:

Gaëtan Renaudeau

GaëtanRenaudeau

...

...

You can customize errors pages depending of the http code error (404, 500, 403, ...) by editing app/views/errors/{code}.html files where {code} is you http code. If you are using other format than html (like xml, json) you can have 404.json , 404.xml, etc...

您可以通过编辑app / views / errors / {code} .html文件来自定义错误页面,具体取决于http代码错误(404,500,403,...),其中{code}是您的http代码。如果你使用的是其他格式而不是html(如xml,json),你可以拥有404.json,404.xml等...

So, modify:

所以,修改:

app/views/errors/404.type_of_response

Hopefully this at least points you in the right direction.

希望这至少可以指出你正确的方向。