Micronaut HttpResponse主体CompositeByteBuf类型引发io.netty.util.IllegalReferenceCountException:refCnt:0

时间:2019-12-04 06:08:50

标签: java netty httpresponse micronaut micronaut-client

我正在使用Micronaut @Client致电外部服务,该服务返回了我  类型为FullNettyClientHttpResponse且其主体为CompositeByteBuf(freed, components=1);的形式,我想将CompositeByteBuf转换为人类可读的toString消息,但它以IllegalReferenceCountException失败。请提供建议,以了解如何在此处获取短信。

@Client(value = "url")
public interface MyClient {

    @Post(consumes = MediaType.APPLICATION_XML, produces = MediaType.APPLICATION_XML)
    HttpResponse call(String body);
}


class service{

void method(){
  HttpResponse httpResponse = client.call(request);// returns FullNettyClientHttpResponse with body "Optional[CompositeByteBuf(freed, components=1)]"
  Optional<CompositeByteBuf> reBody = httpResponse.getBody(CompositeByteBuf.class);
  if(reBody.isPresent()){
      CompositeByteBuf b=reBody.get();
      byte[] req = new byte[b.readableBytes()];
      b.readBytes(req);
      String body = new String(req, CharsetUtil.UTF_8).substring(0, req.length - 
      System.getProperty("line.separator").length());
      System.out.println("server receive order : " + body);
 }
}

我尝试使用toString来获取消息,但是失败,并出现IllegalReferenceCountException。

b.toString(Charset.defaultCharset()); // Method threw 'io.netty.util.IllegalReferenceCountException' exception.

toString返回CompositeByteBuf。

b.toString(); //CompositeByteBuf(freed, components=1);

1 个答案:

答案 0 :(得分:1)

如果您想让Micronaut保留响应的正文,则必须在客户端中指定正文类型。

例如:

HttpResponse<String> call(String body);