Search code examples
javanettyhttpresponsemicronautmicronaut-client

Micronaut HttpResponse body CompositeByteBuf type throws io.netty.util.IllegalReferenceCountException: refCnt: 0


I am using Micronaut @Client to call external service which returns me response of type FullNettyClientHttpResponse and it has body in the form of CompositeByteBuf(freed, components=1); I want to convert CompositeByteBuf to a human readable toString message but it has failing with IllegalReferenceCountException. Please provide suggestion how I can get the text message here.

@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);
 }
}

I tried to get the message using toString but failed with IllegalReferenceCountException.

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

toString returns CompositeByteBuf.

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

Solution

  • You must specify the body type in the client if you want micronaut to keep the body of the response.

    For example:

    HttpResponse<String> call(String body);