How to use PayloadLoggingInterceptor and SOAPLoggingInterceptor interceptors while consuming a third party web service

谁都会走 提交于 2019-12-04 20:23:23

I am adding here solution which I have implemented. There are two ways by which we can implement this solution. I have implemented the second one from below list using JAXBContext and Marshaller.

1> Log Request/Response By interceptor.

We can not use PayloadLoggingInterceptor or SOAPLoggingInterceptor when we are consuming the web service.

We need to use ClientInterceptor when we are consuming the web service. ClientInterceptor is implemented by PayloadValidatingInterceptor class which is used to intercept the request/response and validate the it based on xsd schema.

For that we need to provide interceptor reference as below :

<bean id="MyPayloadValidatingInterceptor" class="com.equifax.ic.datasource.jumio.ws.logging.interceptor.JumioPayloadValidatingInterceptor">
        <property name="schema" value="file:WebContent/WEB-INF/schemas/account-balance-service.xsd" />
        <property name="validateRequest" value="false" />
        <property name="validateResponse" value="false" />
    </bean>
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate"> 
        <constructor-arg ref="soapMessageFactory"/> <property name="marshaller" ref="marshaller"/> 
        <property name="unmarshaller" ref="marshaller"/> 
        <property name="defaultUri"   value="https://partnerstg.duke-energy.com:4443/DukCISSendCreditStatus?wsdl"/>     
        <property name="interceptors">
            <list>              
                <ref bean="MyPayloadValidatingInterceptor"/>                
            </list>         
        </property>     

    </bean>

2> Log Request/Response by using JAXBContext

This is the solution which I have implemented in my application as we should not use PayloadValidatingInterceptor only to log reqeust/response.

private void logJAXBRequest(JAXBElement<CreditStatusMsgType> creditStatusMessageJaxbElement){
        LOGGER.debug("Logging Web Service Request ...");

        StringWriter writer = null;
        StreamResult streamResult = null;
        StringBuffer buffer = null;
        try{
             writer = new StringWriter();
             streamResult = new StreamResult(writer);

             JAXBContext jaxbContext = JAXBContext.newInstance(CreditStatusMsgType.class);
             Marshaller marshaller = jaxbContext.createMarshaller();
             marshaller.marshal(creditStatusMessageJaxbElement, streamResult);

             buffer = writer.getBuffer();

             LOGGER.debug("JAXB Webservice Request : "+ buffer.toString());

             writer.close();

        }catch(Exception ex){
            LOGGER.error("Exception generated while creating XML Logs of JAXB Request :",ex);
        }
    }

He everyone, colleagues. There are two main ways to show XML requests / responses:

  1. First of all you have to add log4j dependency into your pom.xml file:

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    
  2. Then you have to place log4j.properties file into a classpath of your application. When I develop SOAP services I often use Spring WS Maven artefact. Unfortunately, a usual resources folder is not created from scratch and you have to create it manually. Then you place log4j.properties file there. The contents of log4j config depends on the approach you want to use (see items below). Obtained structure is following:

  3. Use a standard Message Logging and Tracing approach & log4j.properties file. Nothing should be configured, developed, written except log4j config file contents. The contents of log4j config should be the following (use these contents as is):

    log4j.rootCategory=DEBUG, stdout
    log4j.logger.org.springframework.ws.client.MessageTracing.sent=TRACE
    log4j.logger.org.springframework.ws.client.MessageTracing.received=DEBUG
    
    log4j.logger.org.springframework.ws.server.MessageTracing=DEBUG
    
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%p [%c{3}] %m%n
    
  4. Use a PayloadLoggingInterceptor & log4j.properties file. Some config changes should be applied, but this approach is more flexible, as for me. First of all you have to add PayloadLoggingInterceptor into a MessageDispatcherServlet config file:

        <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:context="http://www.springframework.org/schema/context"      xmlns:sws="http://www.springframework.org/schema/web-services"        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd        http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <context:component-scan base-package="com.ln.springws"/>
    
        <sws:annotation-driven/>
    
        <sws:interceptors>
            <bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor" />
        </sws:interceptors>
    
        <sws:dynamic-wsdl id="holiday" portTypeName="HumanResource"        locationUri="http://localhost:8080/holidayService/"        targetNamespace="http://spring-ws-holidays.com/hr/definitions">
            <sws:xsd location="/WEB-INF/hr.xsd"/>
        </sws:dynamic-wsdl>
        </beans>
    

    And at last place the following contents to log4j.properties file:

    log4j.rootLogger=debug, stdout
    
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%-5p [%c] - <%m>%n
    
  5. And as a result of both approaches you will have something like that in a console:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!