当我将这样的内容放入我的路由文件中时:
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/