PlayFramework 2.2 Java Action Composition [英] PlayFramework 2.2 Java Action Composition

查看:229
本文介绍了PlayFramework 2.2 Java Action Composition的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Play上使用Action合成!直到现在的应用程序,他们工作正常。然而,最近的2.2.0更新它们不再起作用,我不知道如何正确更新它们。

I've been using Action composition on my Play! apps until now, and they worked fine. However with the recent 2.2.0 update they no longer work and I don't know how to updated them correctly.

这个动作例如:

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

现在问题是call方法应该返回承诺而不是结果,我无法找到一种方法来返回一个简单的JSON消息,而不需要做很多代码,无用的代码因为我正在创建虚拟函数只是为了拥有Promises。必须有一个我没有看到的更好的方法,请指教。

Problem is now the "call" method should return "Promise" instead of "Result", and I can't figure out a way to return a simple JSON message without doing a LOT of code, useless code because I'm creating dummy functions just to have Promises. There has to be a better way I'm not seeing, please advise.

推荐答案

我为我的问题找到了更好的解决方案。它如下:

I figured out a better solution for my problem. It is as follows:

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

这篇关于PlayFramework 2.2 Java Action Composition的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆