问题
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