我有一个简单的finagle服务如下:
import com.twitter.finagle.{Http,Service}
import com.twitter.util.{Await, Future}
import java.net.InetSocketAddress
import org.jboss.netty.handler.codec.http._
import org.jboss.netty.buffer.ChannelBuffers.copiedBuffer
import org.jboss.netty.util.CharsetUtil
import net.liftweb.json._
import com.twitter.finagle.http.{Request,Response,RichHttp}
import com.twitter.finagle.builder.ServerBuilder
case class Add(num1:Int, num2:Int)
case class Result(result:Int)
trait Transformer[R1,T,U,R2] {
def extractPayload(r:R1):T
def responsePayload(u:U):R2
}
object Transformer{
implicit object AddPayloadTransformer
extends Transformer[HttpRequest,Add,Result,HttpResponse]{
implicit val formats = DefaultFormats
def extractPayload(r:HttpRequest):Add = {
val body = r.getContent
val length = body.readableBytes()
val bytes = new Array[Byte](length)
body.getBytes(body.readerIndex(),bytes,0,length)
val bodyStr = new String(bytes)
parse(bodyStr).extract[Add]
}
def responsePayload(u:Result):HttpResponse = {
val result = u.result
val response = Response(HttpVersion.HTTP_1_1,
HttpResponseStatus.OK)
response.setContentTypeJson()
response.setContentString(
s"""
{
"result": $result
}
""")
response.httpResponse
}
}
}
object AdditionServer extends App with Transformer[HttpRequest,Add,Result,HttpResponse]{
private def add(add:Add):Result = Result(add.num1 + add.num2)
private def additionHandler(req:HttpRequest): HttpResponse = {
val addPayload = extractPayload(req)
val result = add(addPayload)
responsePayload(result)
}
val service = new Service[HttpRequest,HttpResponse] {
def apply(req: HttpRequest): Future[HttpResponse] =
{
Future.value(additionHandler(req))
}
}
val server = Http.serve(":8080", service)
Await.ready(server)
println("Started service")
}
当我尝试编译它时,我得到以下异常:
AdditionService.scala:49: object creation impossible, since:
[error] it has 2 unimplemented members.
[error] /** As seen from object AdditionServer, the missing signatures are as follows.
[error] * For convenience, these are usable as stub implementations.
[error] */
[error] def extractPayload(r: org.jboss.netty.handler.codec.http.HttpRequest): Add = ???
[error] def responsePayload(u: Result): org.jboss.netty.handler.codec.http.HttpResponse = ???
[error] object AdditionServer extends App with Transformer[HttpRequest,Add,Result,HttpResponse]{
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
据我所知,我正在以适当的签名来实现这些特征。
有人可以指出这里可能出现的问题。
由于
答案 0 :(得分:2)
AdditionServer
是扩展Transformer
的对象。在您的代码中,此对象既未定义extractPayload
也未定义responsePayload
,因此无法定义编译错误。
您应该将方法定义从特征伴随对象移动到AdditionServer
object AdditionServer extends App with
Transformer[HttpRequest,Add,Result,HttpResponse] {
import Transformer._
def extractPayload(r:HttpRequest):Add = {
val body = r.getContent
val length = body.readableBytes()
val bytes = new Array[Byte](length)
body.getBytes(body.readerIndex(),bytes,0,length)
val bodyStr = new String(bytes)
parse(bodyStr).extract[Add]
}
def responsePayload(u:Result):HttpResponse = {
val result = u.result
val response = Response(HttpVersion.HTTP_1_1,
HttpResponseStatus.OK)
response.setContentTypeJson()
response.setContentString(
s"""
{
"result": $result
}
""")
response.httpResponse
}
}
如果您希望保留在随播对象中定义的方法,可以从AdditionServer
引用它们:
object Transformer{
implicit object AddPayloadTransformer
extends Transformer[HttpRequest,Add,Result,HttpResponse]{
implicit val formats = DefaultFormats
def extractPayload(r:HttpRequest):Add = {
val body = r.getContent
val length = body.readableBytes()
val bytes = new Array[Byte](length)
body.getBytes(body.readerIndex(),bytes,0,length)
val bodyStr = new String(bytes)
parse(bodyStr).extract[Add]
}
def responsePayload(u:Result):HttpResponse = {
val result = u.result
val response = Response(HttpVersion.HTTP_1_1,
HttpResponseStatus.OK)
response.setContentTypeJson()
response.setContentString(
s"""
{
"result": $result
}
""")
response.httpResponse
}
}
}
object AdditionServer extends App with
Transformer[HttpRequest,Add,Result,HttpResponse]{
def extractPayload(r:HttpRequest):Add = Transformer.extractPayload(r)
def responsePayload(u:Result):HttpResponse = Transformer.responsePayload(u)
...
...