【发布时间】:2019-12-04 06:08:50
【问题描述】:
我正在使用 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);
【问题讨论】:
-
该服务以 text/xml 格式返回数据,我预计将使用 application/xml 类型,这是第一个问题,第二个问题我已将响应正文更新为 Single
> 和它解决了这个问题。
标签: java netty httpresponse micronaut micronaut-client