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);
2787184
  • 3,749
  • 10
  • 47
  • 81
  • The service was returning data in text/xml and I was expected to consume in application/xml type, this was the first issue and second I have updated response body to Single> and it resolved the issue. – 2787184 Dec 05 '19 at 04:23

1 Answers1

1

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

James Kleeh
  • 12,094
  • 5
  • 34
  • 61