Java Spring:记录传出的HTTP请求

Java Spring:记录传出的HTTP请求,java,spring,spring-ws,Java,Spring,Spring Ws,我试图在基于spring的web应用程序中记录所有传出的Http请求。是否有用于此目的的拦截器?我想在它离开应用程序之前记录所有传出的内容和标题。我正在使用springws发送SOAP请求。因此,基本上,我不仅要记录SOAP请求xml(如本文所述),还要记录整个http请求。使用WebServiceGatewaySupport上的ClientInterceptor截取请求/响应: // soapClient extends WebServiceGatewaySupport soapClient.

我试图在基于spring的web应用程序中记录所有传出的Http请求。是否有用于此目的的拦截器?我想在它离开应用程序之前记录所有传出的内容和标题。我正在使用
springws
发送SOAP请求。因此,基本上,我不仅要记录SOAP请求xml(如本文所述),还要记录整个http请求。

使用
WebServiceGatewaySupport上的
ClientInterceptor
截取请求/响应:

// 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;
        }
        ...
要同时获取标题,您需要一个
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;
    }
}

使用
WebServiceGatewaySupport
上的
ClientInterceptor
截取请求/响应:

// 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;
        }
        ...
要同时获取标题,您需要一个
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;
    }
}

你考虑过使用aop吗?你考虑过使用aop吗?我已经试过这个了。它只记录SOAP请求xml。不是HTTP请求和HTTP头。您可以传递
TransportOutputStream
,而不是
ByteArrayOutputStream
。根据
WebServiceMessage#writeTo
的Javadoc:如果给定流是{@link*org.springframework.ws.transport.TransportOutputStream}的实例,相应的头也将被写入。我如何在这里获得
TransportOutputStream
的实例?您需要子类
TransportOutputStream
(没有可用的公共实现)。我添加了一个您可以使用的示例实现。我已经尝试过这个。它只记录SOAP请求xml。不是HTTP请求和HTTP头。您可以传递
TransportOutputStream
,而不是
ByteArrayOutputStream
。根据
WebServiceMessage#writeTo
的Javadoc:如果给定流是{@link*org.springframework.ws.transport.TransportOutputStream}的实例,相应的头也将被写入。我如何在这里获得
TransportOutputStream
的实例?您需要子类
TransportOutputStream
(没有可用的公共实现)。我添加了一个示例实现,您可以使用。