简体   繁体   English

scalaz的Liftweb返回响应

[英]Liftweb return response from scalaz

I have a next code, which return: 我有下一个代码,返回:

 import com.twitter.util.{Future, NonFatal}
 import net.liftweb.http.{LiftResponse, OkResponse}

 def service(str: Strign) : Future[ValidationNel[String, LiftResponse]] = {
   if str == "ok"
     Future(OkResponse().successNel[String])
   else
     Future.value("Invalid string".failureNel[LiftResponse]) 
 }

 //and routes for it 

 serve {
   case "user" :: str :: _ Get _ => 
     service(str)   
 } 

but when i run, got No implicit view available from com.twitter.util.Future[scalaz.ValidationNel[String,net.liftweb.http.LiftResponse]] => net.liftweb.http.LiftResponse. 但是当我运行时, No implicit view available from com.twitter.util.Future[scalaz.ValidationNel[String,net.liftweb.http.LiftResponse]] => net.liftweb.http.LiftResponse. How to convert this to ListResponse? 如何将其转换为ListResponse?

Well the error basically says that Lift can't render 错误基本上表明Lift无法渲染

com.twitter.util.Future[scalaz.ValidationNel[String,net.liftweb.http.LiftResponse]]

(At compile time.) To solve your problem you probably have to decompose your task. (在编译时。)要解决您的问题,您可能必须分解您的任务。 First, find out whether Lift can render that "Validation" thing: 首先,找出Lift是否可以渲染“验证”内容:

scalaz.ValidationNel[String,net.liftweb.http.LiftResponse]

If it can't then you'll have to teach Lift. 如果不能,那么你就必须教电梯。 So you should create an implicit (visible in the scope) with the type signature ValidationNel[***] => LiftResponse . 因此,您应该创建一个类型为ValidationNel[***] => LiftResponse的隐式(在范围内可见)。 You can probably just convert this to a Box , which has a default way of rendering an error (empty box). 您可能只是将其转换为Box ,它具有呈现错误的默认方式(空框)。 Or you should write your custom code with "if" and 2 branches, one of which is BadResponse and another is the success. 或者,您应该使用“ if”和2个分支编写自定义代码,其中之一是BadResponse ,另一个是成功。

Another task would be to check whether Lift knows hot to render twitter-s Future -s. 另一个任务是检查Lift是否知道如何渲染twitter-s Future -s。 Again, you should create a simple data and try to render it out. 同样,您应该创建一个简单的数据并尝试将其呈现出来。 Like Future(OkResponse()) . Future(OkResponse()) In order to teach Lift about twitter Futures you probably would have to implement an implicit again, with the type signature Future[T] => LiftResponse . 为了向Lift学习有关Twitter Futures的知识,您可能必须再次使用类型为Future[T] => LiftResponse的类型来实现隐式。 That would probably involve a RestContinuation . 那可能涉及RestContinuation

Lift now supports async calls with LAFuture. 现在,Lift支持使用LAFuture进行异步调用。 Here a sample code: 这里是一个示例代码:

case "delay" :: Nil Get _ =>
  LAFuture.build({
    Thread.sleep(2000)
    <b>Hello</b>
})

When you run it and access /delay the request will be executed asynchronously at the server. 当您运行它并访问/ delay时,请求将在服务器上异步执行。

Of course you will not explicitly call to Thread.sleep in your real code, this is only for sample purposes. 当然,您不会在实际代码中显式调用Thread.sleep,这仅是出于示例目的。

The complete project for this sample code is found at https://github.com/dpp/lift_30_samples (with a couple of quick fixes here -> https://github.com/listatree/lift_30_samples ). 有关此示例代码的完整项目,请参见https://github.com/dpp/lift_30_samples (此处有一些快速修复-> https://github.com/listatree/lift_30_samples )。

Now regarding the transformations you are trying to perform, I would recommend you to use Lift's CanBind functionality. 现在,关于您要执行的转换,我建议您使用Lift的CanBind功能。 Here a sample about them: https://github.com/fmpwizard/lift_starter_2.4/tree/la-futures-2 that also uses Lift's LAFuture but make some transformations with it (By the way those transformations are taking in place automatically as for Lift 2.6 so use this CanBind sample just as a reference for the transformations in question here) 这里是关于它们的一个示例: https : //github.com/fmpwizard/lift_starter_2.4/tree/la-futures-2 ,它也使用Lift的LAFuture,但会进行一些转换(通过这些方式,对于Lift 2.6,因此请使用此CanBind示例作为此处相关转换的参考)

Lift also supports LAFuture for snippet rendering a quick sample for that is: Lift还支持LAFuture的片段渲染快速示例,它是:

def render = "*" #> LAFuture.build({
  Thread.sleep(2000)
  <div>Hello</div>
})

This will use the Lift's Comet infrastructure to make the snippet rendering asynchronous. 这将使用Lift的Comet基础结构来使代码段呈现异步。

Also you can make use of parallel snippets that requires you to include the argument parallel to true as in: 您还可以使用并行代码段,这些代码段要求您包含与true平行的参数,如下所示:

<div data-lift="HelloWorld.howdy?parallel=true">The time is: <span id="time"></span</div>

The parallel snippets works nicely but they consume precious threads, Lift still needs a solution for real non-blocking snippet rendering at server side. 并行代码片段运行良好,但是它们消耗了宝​​贵的线程,Lift仍需要一种在服务器端实现真正的非阻塞代码片段呈现的解决方案。 Let's see what's coming for Lift 3. 让我们来看看Lift 3会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM