2016-06-08 66 views
0

我试图让Scala中使用netty.IO一个HTTP响应(JSON)的物体阻挡网状IO(阻塞,不异步)使用Scala的

大多数示例之类的东西,我觉得是Java和我似乎无法找到一种方法来“回应http身体”的回应。我总是以响应代码或不是身体的对象的.toString结尾。

下面是我到目前为止

def getRestContentFromNetty(url:String) : String ={ 
// IO.netty! 

    val response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); 
    response.headers().set(Names.CONTENT_TYPE, accept_val).set(Names.AUTHORIZATION, basic_auth) 

    val bbuf = Unpooled.copiedBuffer("{\"jsonrpc\":\"2.0\",\"method\":\"calc.add\",\"params\":[1,2],\"id\":1}", StandardCharsets.UTF_8); 
    response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, bbuf.readableBytes()); 
val q = response.content().clear().writeBytes(bbuf); 
    logInfo(response.getStatus.toString)// -> this logs "200 OK", so the response seems good. 

    q.toString 

    } 

这将返回 “UnpooledHeapByteBuf(ridx:0,widx:59,上限:64)”,而不是身体

什么是非常重要的概念,我在这里失踪?

回答

0

如果你要打印的ByteBuf为字符串,需要使用:

q.toString(Charset)