如何从传出拦截器的消息中获取REST服务的响应有效载荷

如何从传出拦截器的消息中获取REST服务的响应有效载荷

本文介绍了如何从传出拦截器的消息中获取REST服务的响应有效载荷?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在SEND阶段在传出链中编写了一个拦截器,并且想要将 REST 服务的响应的有效负载获取到String变量中.我该如何实现?

I have written a interceptor in the outgoing chain in the SEND phase and I want to get the payload of the response of my REST service into a String variable. How can I achieve this?

这是我的拦截器

public class MyLoginOutInterceptor extends LoggingOutInterceptor {

    public MyLoginOutInterceptor() {
        super(Phase.SEND);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        OutputStream os = message.getContent(OutputStream.class);
    }
}

当我在OutputStream os = message.getContent(OutputStream.class);上设置断点时,我看到有效负载在os中,但是我不知道如何将其放入字符串中.

When I put a breakpoint to the OutputStream os = message.getContent(OutputStream.class); I see that the payload is in the os but I don't know how to get it into a String.

有什么想法吗?

推荐答案

在responsePayload中,您应该拥有想要的东西.

In the responsePayload you should have what you want.

public void handleMessage(Message message) throws Fault {
       OutputStream os = message.getContent(OutputStream.class);
       StringBuilder responsePayload = new StringBuilder();
       CachedOutputStream cos = (CachedOutputStream) os;
       try {
               cos.writeCacheTo(responsePayload);
       } catch (IOException e) {
               e.printStackTrace();
       }
}

这篇关于如何从传出拦截器的消息中获取REST服务的响应有效载荷?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 23:54