How do I get the response payload of a REST service from the Message of an outgoing Interceptor?

房东的猫 提交于 2020-01-05 02:35:08

问题


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?

Here is my interceptor

public class MyLoginOutInterceptor extends LoggingOutInterceptor {

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

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

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.

Any ideas?


回答1:


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


来源:https://stackoverflow.com/questions/21018787/how-do-i-get-the-response-payload-of-a-rest-service-from-the-message-of-an-outgo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!