2013-09-25 11 views
6

Uso la composizione di Action sul mio Play! app fino ad ora, e hanno funzionato bene. Tuttavia con il recente aggiornamento 2.2.0 non funzionano più e non so come aggiornarli correttamente.PlayFramework 2.2 Java Action Composition

che questa azione, ad esempio:

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); 
} 
} 

problema è ora il metodo "chiamata" dovrebbe restituire "Promessa" al posto di "Risultato", e non riesco a trovare un modo per restituire un semplice messaggio JSON senza fare un sacco di codice, codice inutile perché sto creando funzioni fittizie solo per avere Promesse. Deve esserci un modo migliore che non vedo, per favore avvisare.

risposta

9

Ho trovato una soluzione migliore per il mio problema. È il seguente:

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