neb*_*ula 1 java spring spring-ws
我正在尝试在基于 Spring 的 Web 应用程序中记录所有传出的 Http 请求。是否有用于此目的的拦截器?我想在离开应用程序之前记录所有传出的内容和标题。我正在使用spring-ws发送 SOAP 请求。所以基本上,我不仅要记录 SOAP 请求 xml(如这里提到的如何使 Spring WebServices 记录所有 SOAP 请求?),还要记录整个 http 请求。
使用ClientInterceptor上的拦截请求/响应WebServiceGatewaySupport:
// soapClient extends WebServiceGatewaySupport
soapClient.setInterceptors(new ClientInterceptor[]{new ClientInterceptor() {
@Override
public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
messageContext.getRequest().writeTo(os);
} catch (IOException e) {
throw new WebServiceIOException(e.getMessage(), e);
}
String request = new String(os.toByteArray());
logger.trace("Request Envelope: " + request);
return true;
}
@Override
public boolean handleResponse(MessageContext messageContext) throws WebServiceClientException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
messageContext.getResponse().writeTo(os);
} catch (IOException e) {
throw new WebServiceIOException(e.getMessage(), e);
}
String response = new String(os.toByteArray());
logger.trace("Response Envelope: " + response);
return true;
}
...
Run Code Online (Sandbox Code Playgroud)
要获取标题,您还需要一个TransportOutputStream. 不幸的是,该类是抽象的,因此您需要对其进行子类化。这是它的外观:
class ByteArrayTransportOutputStream extends TransportOutputStream {
private ByteArrayOutputStream outputStream;
@Override
public void addHeader(String name, String value) throws IOException {
createOutputStream();
String header = name + ": " + value + "\n";
outputStream.write(header.getBytes());
}
public byte[] toByteArray() {
return outputStream.toByteArray();
}
@Override
protected OutputStream createOutputStream() throws IOException {
if (outputStream == null) {
outputStream = new ByteArrayOutputStream();
}
return outputStream;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4572 次 |
| 最近记录: |