0

我正在使用 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);
4

1 回答 1

1

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

例如:

HttpResponse<String> call(String body);

于 2019-12-04T17:19:18.217 回答