How can I make Spring WebServices log all SOAP requests?

前端 未结 7 1958
遇见更好的自我
遇见更好的自我 2020-12-04 16:43

I need all SOAP requests logged in the CommonLogFormat (see http://en.wikipedia.org/wiki/Common_Log_Format), plus the duration (the amount of time it takes to p

相关标签:
7条回答
  • 2020-12-04 17:15

    If you have your own Logging system the following interceptor can be an alternative to log the SOAP messages.

        setInterceptors(new ClientInterceptor[]{new ClientInterceptor() {
    
            @Override
            public boolean handleResponse(MessageContext messageContext) throws WebServiceClientException {
                System.out.println("### SOAP RESPONSE ###");
                try {
                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                    messageContext.getResponse().writeTo(buffer);
                    String payload = buffer.toString(java.nio.charset.StandardCharsets.UTF_8.name());
                    System.out.println(payload);
                } catch (IOException e) {
                    throw new WebServiceClientException("Can not write the SOAP response into the out stream", e) {
                        private static final long serialVersionUID = -7118480620416458069L;
                    };
                }
    
                return true;
            }
    
            @Override
            public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
    
                System.out.println("### SOAP REQUEST ###");
                try {
                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                    messageContext.getRequest().writeTo(buffer);
                    String payload = buffer.toString(java.nio.charset.StandardCharsets.UTF_8.name());
                    System.out.println(payload);
                } catch (IOException e) {
                    throw new WebServiceClientException("Can not write the SOAP request into the out stream", e) {
                        private static final long serialVersionUID = -7118480620416458069L;
                    };
                }
    
                return true;
            }
    
            @Override
            public boolean handleFault(MessageContext messageContext) throws WebServiceClientException {
                System.out.println("### SOAP FAULT ###");
                try {
                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                    messageContext.getResponse().writeTo(buffer);
                    String payload = buffer.toString(java.nio.charset.StandardCharsets.UTF_8.name());
                    System.out.println(payload);
                } catch (IOException e) {
                    throw new WebServiceClientException("Can not write the SOAP fault into the out stream", e) {
                        private static final long serialVersionUID = 3538336091916808141L;
                    };
                }
    
                return true;
            }
        }});
    

    In each handle method you can easily use payload to obtain raw soap messages.

    0 讨论(0)
提交回复
热议问题