Spring MVC 如何让Spring WebServices记录所有SOAP请求?

zed5wv10  于 2022-11-14  发布在  Spring
关注(0)|答案(9)|浏览(166)

我需要在CommonLogFormat中记录所有SOAP请求(请参见http://en.wikipedia.org/wiki/Common_Log_Format),以及持续时间(处理请求所需的时间)。
最好的方法是什么?看起来可以为Spring WebServices配置log4j,但是它会记录我感兴趣的所有值吗?http://pijava.wordpress.com/2009/12/04/spring-webservice-soap-requestresponse-logging-with-log4j/
编辑:我们实际上使用的是SLF4J,而不是Log4j。而且,看起来可以通过配置PayloadLoggingInterceptor来实现这一点:http://static.springsource.org/spring-ws/site/reference/html/server.html#server-endpoint-interceptor
但是我不确定日志消息会去哪里。我把那个拦截器添加到我们的拦截器中,我没有看到任何日志消息。

p4rjhz4m

p4rjhz4m1#

对于**Sping Boot **项目,在application.properties中添加以下内容对我有效:

logging.level.org.springframework.web=DEBUG
logging.level.org.springframework.ws.client.MessageTracing.sent=DEBUG
logging.level.org.springframework.ws.server.MessageTracing.sent=DEBUG
logging.level.org.springframework.ws.client.MessageTracing.received=TRACE
logging.level.org.springframework.ws.server.MessageTracing.received=TRACE
qvsjd97n

qvsjd97n2#

您可以使用它来记录传入和传出Web服务调用的原始有效负载。我不知道如何记录Web服务通信所用的时间。

<!-- Spring Web Service Payload Logging-->
   <logger name="org.springframework.ws.client.MessageTracing">
    <level value="TRACE"/> 
   </logger>
   <logger name="org.springframework.ws.server.MessageTracing">
    <level value="TRACE"/> 
   </logger>

更多详细信息请访问http://static.springsource.org/spring-ws/site/reference/html/common.html#logging

tjrkku2a

tjrkku2a3#

这对我很有效。它记录了发送的请求消息和收到的响应。你可以从日志中计算出所用的总时间。

log4j.logger.org.springframework.ws.client.MessageTracing.sent=TRACE
log4j.logger.org.springframework.ws.client.MessageTracing.received=TRACE
gupuwyp2

gupuwyp24#

如果您有自己的日志记录系统,则可以使用以下拦截器来记录SOAP消息。

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

在每个句柄方法中,您可以轻松地使用payload来获取原始soap消息。

yuvru6vn

yuvru6vn5#

首先,SLF 4J只是一个简单的外观,这意味着您仍然需要一个日志框架(例如java.util.logging、logback、log4j)。
其次,Spring-ws使用了Commons Logging接口,这是另一个类似于SLF 4J的简单外观。
最后,您可以使用下面的设置来启用Spring-ws消息日志功能。

log4j.logger.org.springframework.ws.client.MessageTracing.sent=DEBUG
log4j.logger.org.springframework.ws.client.MessageTracing.received=TRACE

log4j.logger.org.springframework.ws.server.MessageTracing.sent=DEBUG
log4j.logger.org.springframework.ws.server.MessageTracing.received=TRACE
2admgd59

2admgd596#

log4j.properties文件中包含以下内容...
1.要记录所有服务器端消息,请执行下列操作:log4j.logger.org.springframework.ws.server.MessageTracing=DEBUG
1.记录所有客户端消息:log4j.logger.org.springframework.ws.client.MessageTracing=TRACE
DEBUG级别-仅记录有效负载根元素
TRACE级别-记录整个消息内容
最后,要仅记录发送或接收的消息,请在配置结束时使用.sent.received
例如:log4j.logger.org.springframework.ws.server.MessageTracing.received=DEBUG记录客户端接收的消息有效负载根元素,返回:

DEBUG WebServiceMessageReceiverHandlerAdapter:114 - Accepting incoming [org.springframework.ws.transport.http.HttpServletConnection@51ad62d9] to [http://localhost:8080/mock-platform/services]

更多信息

gr8qqesn

gr8qqesn7#

将servlet过滤器添加到web.xml中的springws(以便与org.springframework.web.servlet.DispatcherServlet一起移动
您可以在这里找到筛选器http://www.wetfeetblog.com/servlet-filer-to-log-request-and-response-details-and-payload/431
在过滤器内部,您可以根据需要进行日志记录

kq4fsx7k

kq4fsx7k8#

. . .
package com.example.Soap;

import org.springframework.ws.client.WebServiceClientException;
import org.springframework.ws.client.support.interceptor.ClientInterceptor;
import org.springframework.ws.context.MessageContext;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class LoggingVonfig  implements 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;
        }

    @Override
    public void afterCompletion(MessageContext messageContext, Exception e) throws WebServiceClientException {

    }

}

. . . 

This is logging Configuration class

. . . 
package com.example.Soap;

import com.example.Soap.com.example.Soap.Add;
import com.example.Soap.com.example.Soap.AddResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.client.support.interceptor.ClientInterceptor;
import org.springframework.ws.soap.client.core.SoapActionCallback;

public class CalculatorClient  extends WebServiceGatewaySupport {

    private static Logger log = LoggerFactory.getLogger(CalculatorClient.class);

    public com.example.Soap.com.example.Soap.AddResponse getaddition(com.example.Soap.com.example.Soap.Add addrequest){
        com.example.Soap.com.example.Soap.Add add = new com.example.Soap.com.example.Soap.Add();
        add.setIntB(addrequest.getIntB());
        add.setIntA(addrequest.getIntA());
        log.info("----------------------------------------------"+"Inbound Request"+"-----------------------");
        com.example.Soap.com.example.Soap.AddResponse addResponse = (com.example.Soap.com.example.Soap.AddResponse) getWebServiceTemplate().marshalSendAndReceive("http://www.dneonline.com/calculator.asmx?wsdl",add,new SoapActionCallback("http://tempuri.org/Add"));

        return addResponse;
    }
    public com.example.Soap.com.example.Soap.SubtractResponse getSubtract(com.example.Soap.com.example.Soap.Subtract subreq){
        com.example.Soap.com.example.Soap.Subtract subtract=new com.example.Soap.com.example.Soap.Subtract();
        subtract.setIntA(subreq.getIntA());
        subtract.setIntB(subreq.getIntB());
        com.example.Soap.com.example.Soap.SubtractResponse subtractResponse=(com.example.Soap.com.example.Soap.SubtractResponse) getWebServiceTemplate().marshalSendAndReceive("http://www.dneonline.com/calculator.asmx?wsdl",subtract,new SoapActionCallback("http://tempuri.org/Subtract"));
        return  subtractResponse;
    }
    public com.example.Soap.com.example.Soap.MultiplyResponse getMultiply(com.example.Soap.com.example.Soap.Multiply multiply)
    {
        com.example.Soap.com.example.Soap.Multiply multiply1=new com.example.Soap.com.example.Soap.Multiply();
        multiply1.setIntA(multiply.getIntA());
        multiply1.setIntB(multiply.getIntB());
        com.example.Soap.com.example.Soap.MultiplyResponse multiplyResponse=(com.example.Soap.com.example.Soap.MultiplyResponse) getWebServiceTemplate().marshalSendAndReceive("http://www.dneonline.com/calculator.asmx?wsdl",multiply1,new SoapActionCallback("http://tempuri.org/Multiply"));
        return  multiplyResponse;
    }
    public com.example.Soap.com.example.Soap.DivideResponse getDivide(com.example.Soap.com.example.Soap.Divide divide){
        com.example.Soap.com.example.Soap.Divide divide1=new com.example.Soap.com.example.Soap.Divide();
        divide1.setIntA(divide.getIntA());
        divide1.setIntB(divide.getIntB());
        com.example.Soap.com.example.Soap.DivideResponse divideResponse=(com.example.Soap.com.example.Soap.DivideResponse) getWebServiceTemplate().marshalSendAndReceive("http://www.dneonline.com/calculator.asmx?wsdl",divide1,new SoapActionCallback("http://tempuri.org/Divide"));
        return divideResponse;
    }

    public void MySoapClient() {
        this.setInterceptors(new ClientInterceptor[] { new LoggingVonfig() });

    }

}

. . . 
This is my client class

. . . 
package com.example.Soap;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.ws.client.core.WebServiceTemplate;

@Configuration
public class CalculatorConfig {

    @Bean
    public Jaxb2Marshaller marshaller(){
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
//        jaxb2Marshaller.setPackagesToScan("com.example.Soap.com.example.Soap");

        jaxb2Marshaller.setContextPath("com.example.Soap.com.example.Soap"); // this will serilaize and unserialize it
        return jaxb2Marshaller;
    }

    @Bean
    public CalculatorClient calculatorClient(Jaxb2Marshaller jaxb2Marshaller){
        WebServiceTemplate wsTemplate = new WebServiceTemplate();
        CalculatorClient calculatorClient = new CalculatorClient();
        calculatorClient.setDefaultUri("http://www.dneonline.com");
        calculatorClient.setMarshaller(jaxb2Marshaller);
        calculatorClient.setUnmarshaller(jaxb2Marshaller);

        return calculatorClient;
    }

}

. . .

configuration file

. . . 

package com.example.Soap;

import com.example.Soap.com.example.Soap.Add;
import com.example.Soap.com.example.Soap.AddResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SoapApplication {

    @Autowired
    private CalculatorClient calculatorClient;

    @PostMapping(value = "/add")
    public com.example.Soap.com.example.Soap.AddResponse addelements(@RequestBody com.example.Soap.com.example.Soap.Add add){
        return calculatorClient.getaddition(add);
    }
    @PostMapping(value = "/subtract")
    public com.example.Soap.com.example.Soap.SubtractResponse addelements(@RequestBody com.example.Soap.com.example.Soap.Subtract subreq){
        return calculatorClient.getSubtract(subreq);
    }
    @PostMapping(value = "/multiply")
    public com.example.Soap.com.example.Soap.MultiplyResponse addelements(@RequestBody com.example.Soap.com.example.Soap.Multiply multiply){
        return  calculatorClient.getMultiply(multiply);
    }
    @PostMapping(value = "/divide")
    public com.example.Soap.com.example.Soap.DivideResponse addelements(@RequestBody com.example.Soap.com.example.Soap.Divide divide){
        return calculatorClient.getDivide(divide);
    }

    public static void main(String[] args) {
        SpringApplication.run(SoapApplication.class, args);
    }

}
.  . .

这些都是我的类,但我仍然不能在我的控制台中记录所有的请求和响应。我不知道我做错了什么。我也实现了客户端配置。

g6baxovj

g6baxovj9#

最简单的方法是将属性添加到安全配置(securityPolicy.xml)中:

<xwss:SecurityConfiguration xmlns:xwss="http://java.sun.com/xml/ns/xwss/config" dumpMessages="true">

不需要在application.properties内进行其他设置。
选中“了解安全策略文件”链接。

相关问题