Scala Finch配置对不存在端点的响应

Scala Finch配置对不存在端点的响应,scala,finch,Scala,Finch,我有一个finch(0.11.1)应用程序在本地机器上运行,如果我有 curl -XPOST localhost:8081/lfdjalfjla // some non-existing url 我得到一个404响应,响应主体为空。 假设我想给出某个错误消息作为响应,我将在哪里配置它? 我还没有在(尚未)中找到答案。我认为合理的实现方法是在.toServiceAs调用返回的服务上应用欺骗过滤器 import com.twitter.finagle.http.{Request, Respons

我有一个finch(0.11.1)应用程序在本地机器上运行,如果我有

curl -XPOST localhost:8081/lfdjalfjla  // some non-existing url
我得到一个404响应,响应主体为空。 假设我想给出某个错误消息作为响应,我将在哪里配置它?
我还没有在(尚未)中找到答案。

我认为合理的实现方法是在
.toServiceAs
调用返回的服务上应用欺骗过滤器

import com.twitter.finagle.http.{Request, Response, Status}
import com.twitter.finagle.{Service, SimpleFilter}
import com.twitter.util.Future

class OnNotFound(rep: Response) extends SimpleFilter[Request, Response] {
  def apply(req: Request, s: Service[Request, Response]): Future[Response] = {
    s(req).map {
      case r if r.status == Status.NotFound => rep
      case r => r
    }
  }
}
然后呢

scala> import io.finch._, com.twitter.finagle.http.Status

scala> val e = get("foo") { Ok("bar") }
e: io.finch.Endpoint[String] = GET /foo

scala> val s = new OnNotFound(Response(Status.InternalServerError)).andThen(e.toServiceAs[Text.Plain])
s: com.twitter.finagle.Service[com.twitter.finagle.http.Request,com.twitter.finagle.http.Response] = <function1>

scala> Http.server.serve(":8080", s)
res0: com.twitter.finagle.ListeningServer = Group(/0:0:0:0:0:0:0:0:8080)

我认为实现这一点的合理方法是在
.toServiceAs
调用返回的服务上应用欺骗过滤器

import com.twitter.finagle.http.{Request, Response, Status}
import com.twitter.finagle.{Service, SimpleFilter}
import com.twitter.util.Future

class OnNotFound(rep: Response) extends SimpleFilter[Request, Response] {
  def apply(req: Request, s: Service[Request, Response]): Future[Response] = {
    s(req).map {
      case r if r.status == Status.NotFound => rep
      case r => r
    }
  }
}
然后呢

scala> import io.finch._, com.twitter.finagle.http.Status

scala> val e = get("foo") { Ok("bar") }
e: io.finch.Endpoint[String] = GET /foo

scala> val s = new OnNotFound(Response(Status.InternalServerError)).andThen(e.toServiceAs[Text.Plain])
s: com.twitter.finagle.Service[com.twitter.finagle.http.Request,com.twitter.finagle.http.Response] = <function1>

scala> Http.server.serve(":8080", s)
res0: com.twitter.finagle.ListeningServer = Group(/0:0:0:0:0:0:0:0:8080)

这看起来很有说服力。谢谢您!这看起来很有说服力。谢谢您!