需要:play.api.mvc.Request [?] => play.api.mvc.Result [英] required: play.api.mvc.Request[?] => play.api.mvc.Result

查看:63
本文介绍了需要:play.api.mvc.Request [?] => play.api.mvc.Result的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在迁移到Play 2.6,并具有以下曾经有效的API包装器功能:

I am migrating to Play 2.6 and have the following API wrapper functions that used to work:

trait API {
    self: Controller =>

    def api(businessLogic: Request[AnyContent] => Any): Action[AnyContent] = apiWithBody(parse.anyContent)(businessLogic)

    def apiWithBody[A](bodyParser: BodyParser[A])(businessLogic: Request[A] => Any): Action[A] = Action(bodyParser) {
        implicit request =>
        {
            val apiResult = businessLogic(request)
            val start = new java.util.Date().getTime
            val actionDuration = (new java.util.Date().getTime - start)
            val response = resultFrom(apiResult, request, actionDuration)    // Returns a Result
            response
        }
    }
}

由Controller函数调用,例如:

Called by Controller functions like:

object Accounts extends Controller with API {

    def all = superUser {
        implicit principal =>
            api {
                request =>
                    models.Account.all
            }
    }
}

其中superUser是主体(用户)类型"admin".

Where superUser is the principal (user) type "admin".

并出现以下编译器错误:

And get the following compiler error:

[error] type mismatch;
[error]  found   : play.api.mvc.Action[play.api.mvc.AnyContent]
[error]  required: play.api.mvc.Request[?] => play.api.mvc.Result
[error]             api {
[error]                 ^

我正在使用sbt 1.1.5和Scala 2.11.8进行构建.

I'm building with sbt 1.1.5 and Scala 2.11.8.

我猜测[?]表示编译器不知道需要哪种类型,但我不明白什么是错误的.我已经搜索了此问题,但没有找到针对该问题的具体答案.

I am guessing the [?] means the compiler doesn't know what type is required but I don't understand what is wrong. I have searched for this issue but not found the specific answer for this problem.

此外,我遇到一个错误:

In addition I'm getting an error:

[error]  could not find implicit value for parameter parser: play.api.mvc.BodyParser[Any]
[error]     def all = superUser {
[error]                         ^

我作为单独问题发布的

(请参阅

that I posted as a separate issue (see could not find implicit value for parameter parser: play.api.mvc.BodyParser[Any]) but might be relevant here?

def superUser[A](f: => Principal => Request[A] => Result)(implicit parser: BodyParser[A]): SecureAction[A] = {
    _superUser {
        user =>
            implicit val principal = data.Principal(user)
            Action(parser)(request => f(principal)(request))
    }
}

private def _superUser[A](action: String => Action[A]) = {
    play.api.mvc.Security.Authenticated(getSuperUser, onUnauthorized)(action)
}

任何帮助将不胜感激.

推荐答案

对不起,我对此体系结构有点困惑:

Sorry I'm little bit confused here about the architecture as:

  1. API调用在哪里?在模型内吗?您是在当前Play应用之外拨打电话吗?为什么不使用Future API?因为这样您可以recover它.然后可以帮助您进行日志记录和错误处理.
  2. all方法获取请求,然后不返回HTTP响应.为什么不传递请求中的需求(例如Cookie).
  3. 我认为,您的问题的答案是将您的行动构成与行动结合起来.像这样:

  1. Where is the API call? Is it within the model? Are you calling outside the current Play app? Why don't you use the Future API for it? Because then you can recover it. Which then help you with logging and error handling.
  2. The all method get the request, and then it does not return an HTTP response. Why don't you pass on what you need from the request (e.g., the Cookie).
  3. I think the answer to your question is to use your action composition with action. Something like:

def myMethod(queryParam: String) = myDefinedAction compose Action { ??? } 

//In case you want to use the Future API, then you need to be async
def myMethod(queryParam: String) = (myDefinedAction compose Action).async {
  implicit request => 
     apiCall.map{
       case _ => Ok("good request") 
     }.recover{case _ => BadRequest} 

} 

这篇关于需要:play.api.mvc.Request [?] => play.api.mvc.Result的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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