2013-09-25 24 views
6

我一直在使用動作組合!直到現在的應用程序,他們工作得很好。然而,最近的2.2.0更新版本不再適用,我不知道如何正確更新它們。PlayFramework 2.2 Java動作組合

,這一行動,例如:

public class ChatMsgValidation extends Action<ChatMsgValidation.ValidChatMsg> { 

@With(ChatMsgValidation.class) 
@Target({ElementType.TYPE, ElementType.METHOD}) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface ValidChatMsg { 
} 


public Result call(Http.Context ctx) throws Throwable { 

    Utils.debugFunctionCall("ValidChatMsg() " + ctx.toString()); 

    // validate we got the "player" parameter 
    JsonNode jsonRequest = request().body().asJson(); 
    if (!WSUtils.validateJSONField(Constants.JSON_MSG, jsonRequest)) { 
     return badRequest(WSUtils.simpleMissingFieldMsg(Constants.JSON_MSG)); 
    } 

    RequestParser requestParser = new RequestParser(request()); 
    String chatMsg = requestParser.getMessage(); 

    if (chatMsg.isEmpty()) { 
     return badRequest(WSUtils.simpleFailureMsgWithReason(Messages.get("message.cannot.be.empty.error"), FailConstants.REASON_EMPTY)); 
    } 


    if (chatMsg.length() < Constants.MIN_CHAT_MESSAGE_LENGTH) { 
     return badRequest(WSUtils.simpleFailureMsgWithReason(Messages.get("query.lower.limit.error"), FailConstants.REASON_TOO_SHORT)); 
    } 

    if (chatMsg.length() > Constants.MAX_CHAT_MESSAGE_LENGTH) { 
     return badRequest(WSUtils.simpleFailureMsgWithReason(Messages.get("message.too.long.error"), FailConstants.REASON_TOO_LONG)); 
    } 

    return delegate.call(ctx); 
} 
} 

問題是現在的「調用」方法應該返回「無極」,而不是「結果」,而我不能想出一個辦法來返回簡單的JSON消息,而不需要執行大量的代碼,因爲我正在創建虛擬函數來實現Promise。有一個更好的方式,我沒有看到,請告知。

回答

9

我想出了一個更好的解決方案,我的問題。它如下:

return F.Promise.pure((SimpleResult) badRequest(WSUtils.simpleFailureMsgWithReason(Messages.get("message.cannot.be.empty.error"), FailConstants.REASON_EMPTY)));