PlayFramework 2.2 Java Action Composition

I am using Action composition on my Play! applications so far and they have worked perfectly. However, with the recent 2.2.0 update, they no longer work, and I don’t know how to update them correctly.

What is this action, for example:

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

Now the problem is that the "call" method should return "Promise" instead of "Result", and I cannot find a way to return a simple JSON message without executing a lot of code, useless code, because I'm creating dummy functions to have Promises . There must be a better way that I do not see, please advise.

+6
source share
1 answer

I understood the best solution for my problem. It looks like this:

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

Source: https://habr.com/ru/post/954619/


All Articles