PlayFramework 1.4 在条件不满足后阻止传播

PlayFramework 1.4 prevent propagation after condition not fulfilled

我有以下问题:我想使用 @Before 拦截请求并检查是否满足特定条件;如果不是,则阻止传播并 return 向用户发送消息。

除了抛出异常还有别的方法吗?

代码示例:

在路线中:

   GET /someRoute  MainController/someMethod
   GET /otherRoute MainController/otherMethod

在控制器中

public class MainController{
@Before
public static void check(){
// checking, if not fulfilled - render error and requested method is not invoked.
}
public static void someMethod() {/*some action*/}
public static void otherMethod() {/*...other action*/}

由于@Before注解的方法是在Controller上的,所以可以直接使用所有的static函数直接回答。 例如,如果您想要 return 纯文本:

public class MainController{
@Before
public static void check(){
  // checking, if not fulfilled - render error and requested method is not invoked.
  renderText("Error");
}
public static void someMethod() {/*some action*/}
public static void otherMethod() {/*...other action*/}

这将停止传播并发送对请求的响应。

例如,您也可以使用 error() 或 forbidden()。