Spring 如何在使用第三方web服务时使用PayloadLoggingInterceptor和SOAPLoggingInterceptor拦截器

Spring 如何在使用第三方web服务时使用PayloadLoggingInterceptor和SOAPLoggingInterceptor拦截器,spring,maven,jaxb,webservice-client,spring-ws,Spring,Maven,Jaxb,Webservice Client,Spring Ws,在我的应用程序中,我使用的是客户机提供的第三方web服务 我已经在Spring和Hibernate框架上开发了我的应用程序,在一个模块中,我使用这个第三方web服务url。我已经使用 javab2 maven插件 中的maven插件在我的pom.xml文件中声明如下: <plugin> <groupId>org.codehaus.mojo</groupId> <artif

在我的应用程序中,我使用的是客户机提供的第三方web服务

我已经在Spring和Hibernate框架上开发了我的应用程序,在一个模块中,我使用这个第三方web服务url。我已经使用

javab2 maven插件

中的maven插件在我的pom.xml文件中声明如下:

          <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <id>xjc</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!-- Package to store the generated file -->
                    <packageName>com.equifax.unsolicited.wsdl.stub</packageName>
                    <!-- Treat the input as WSDL -->
                    <wsdl>true</wsdl>
                    <!-- Input is not XML schema -->
                    <xmlschema>false</xmlschema>
                    <!-- The WSDL file that you saved earlier -->
                    <schemaFiles>Duk_CIS_Send_CreditStatus.wsdl</schemaFiles>
                    <!-- The location of the WSDL file -->
                    <schemaDirectory>${project.basedir}/src/main/wsdl</schemaDirectory>
                    <!-- The output directory to store the generated Java files -->
                    <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                    <!-- Don't clear output directory on each run -->
                    <clearOutputDir>false</clearOutputDir>
                </configuration>
            </plugin>

org.codehaus.mojo
. 此网站在发布web服务时对拦截器进行了解释


请告诉我在使用web服务时是否应该使用此拦截器?或者我应该如何打印属于JAXB元素的请求和响应?

我在这里添加了我已经实现的解决方案。有两种方法可以实现此解决方案。我使用JAXBContext和Marshaller实现了下面列表中的第二个

1>记录拦截器的请求/响应。

在使用web服务时,我们不能使用PayloadLoggingInterceptorSOAPLoggingInterceptor

在使用web服务时,我们需要使用ClientInterceptorClientInterceptorPayloadValidatingInterceptor类实现,该类用于拦截请求/响应,并基于xsd模式对其进行验证

为此,我们需要提供拦截器参考,如下所示:

<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>使用JAXBContext记录请求/响应

这是我在应用程序中实现的解决方案,因为我们不应该仅使用PayloadValidatingInterceptor来记录请求/响应

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);
        }
    }
private void logJAXBRequest(JAXBElement creditStatusMessageJaxbElement){
debug(“记录Web服务请求…”);
StringWriter=null;
StreamResult StreamResult=null;
StringBuffer=null;
试一试{
writer=新的StringWriter();
streamResult=新的streamResult(writer);
JAXBContext JAXBContext=JAXBContext.newInstance(CreditStatusMsgType.class);
Marshaller=jaxbContext.createMarshaller();
marshaller.marshall(creditStatusMessageJaxbElement,streamResult);
buffer=writer.getBuffer();
debug(“jaxbwebservice请求:+buffer.toString());
writer.close();
}捕获(例外情况除外){
error(“创建JAXB请求的XML日志时生成异常:”,ex);
}
}
他欢迎所有人、同事。 显示XML请求/响应的主要方式有两种:

  • 首先,您必须将
    log4j
    依赖项添加到
    pom.xml
    文件中:

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    
    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
    
  • 使用
    PayloadLoggingInterceptor
    log4j.properties
    文件。应该应用一些配置更改,但对我来说,这种方法更灵活。首先,您必须将
    PayloadLoggingInterceptor
    添加到
    MessageDispatcherServlet
    config文件中:

        <?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>
    
  • 这两种方法的结果是,您将在控制台中看到类似的内容:


  • 当然,它们不起作用,因为您必须将它们注册到
    WebserviceTemplate
    您正在将它们添加到端点,如果您有一个不用于客户端的服务器,则可以使用端点。只需设置
    WebserviceTemplate
    interceptors
    属性。如何设置web服务模板的interceptor属性?与您在其上设置的所有其他属性一样。。。为什么会有所不同…当我尝试设置拦截器属性时,出现异常,如下所示。。。请不要添加代码作为注释,这会使它们无法阅读。将元素包装在
    元素中,并确保bean是顶级bean(而不是在
    sws:interceptors
    元素中)。。。
    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
    
        <?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>
    
    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