【问题标题】:PlayFramework 2.2 Java Action CompositionPlayFramework 2.2 Java 动作组合
【发布时间】:2013-09-25 17:17:57
【问题描述】:

我一直在 Play 中使用动作合成!应用程序直到现在,它们运行良好。但是随着最近的 2.2.0 更新,它们不再起作用,我不知道如何正确更新它们。

以这个动作为例:

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

现在的问题是“调用”方法应该返回“Promise”而不是“Result”,而且我想不出一种方法来返回一个简单的 JSON 消息而不做很多代码,无用的代码,因为我创建虚拟函数只是为了拥有 Promise。一定有更好的方法我没有看到,请指教。

【问题讨论】:

    标签: java playframework playframework-2.0


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多