playframework - Play2 : Is it possible to respond with 405 Method not allowed"instead of "404 Not Found" if a route exists, 但不适用于当前方法

标签 playframework

当我将这样的内容放入我的路由文件中时:

GET /foo com.example.controllers.FooController.foo

然后向该资源发出一个不是 GET 的请求,例如POST/foo,默认路由器以 404 和“找不到操作”响应。是否有可能让它检测到不同方法存在资源并输出状态代码 405(方法不允许)?

最佳答案

您需要覆盖 GlobalSettings 类中的 onHandlerNotFound 方法:

Called when no action was found to serve a request. The default behavior is to render the framework's default 404 page. This is achieved by returning null, so that the Scala engine handles onHandlerNotFound. By overriding this method one can provide an alternative 404 page.

更多详情 here .

关于playframework - Play2 : Is it possible to respond with 405 Method not allowed"instead of "404 Not Found" if a route exists, 但不适用于当前方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30210882/

相关文章:

java - Play 中的现场同步! Controller

java - 为什么 Play Framework war 包含重复的类文件?

scala - 在Play 2.1.0中运行现有项目时出现: Could not retrieve sbt 0. 11.3错误?

ssl - Play 框架看不到 SSL 中间/链证书

Java 玩吧! 2.0 eclipse

scala - 没有找到 Scala 的 Json 格式化程序, Play Framework 错误

java - 非静态内部类名作为类型参数

java - Play Framework with Eclipse 启用建议并删除错误

scala - 如何在 Play Framework 中隐藏文本字段

java - 无法在 play 1.2.5 中获取 json?