簡體   English   中英

在 spring 中使用 Apache cxf 攔截器和 log4j 記錄完整的請求和響應

[英]logging full request and response using Apache cxf interceptor and log4j in spring

我正在嘗試在 spring 中使用 Apache cxf、Log4j 來記錄soap web 服務的請求和響應

以下是我的項目結構

在此處輸入圖片說明

我的 cxf.xml 是

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:cxf="http://cxf.apache.org/core" 
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemalocation="http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

   <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" id="loggingInInterceptor" />
   <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" id="logOutInterceptor" />

    <cxf:bus>
        <cxf:ininterceptors>
            <ref bean="loggingInInterceptor" />
        </cxf:ininterceptors>
        <cxf:outinterceptors>
            <ref bean="logOutInterceptor" />
        </cxf:outinterceptors>
    </cxf:bus>



</beans>

我的 bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:cxf="http://cxf.apache.org/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xsi:schemaLocation="http://cxf.apache.org/core 
                         http://cxf.apache.org/schemas/core.xsd
                         http://www.springframework.org/schema/beans 
                         http://www.springframework.org/schema/beans/spring-beans.xsd
                         http://cxf.apache.org/jaxws  
                         http://cxf.apache.org/schemas/jaxws.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <jaxws:endpoint id="bookShelfService"
        implementor="com.test.services.BookShelfServiceImpl" address="/bookshelfservice" />

</beans>

log4j.properties

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\wsimport\\log.txt
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

我的 org.apache.cxf.Logger

org.apache.cxf.common.logging.Log4jLogger

和 web.xml

<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/beans.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 

    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

我已經完成了所有事情,但我仍然沒有獲得帶有標頭的服務的請求和響應日志

我正在使用 tomcat 來部署我的應用程序並使用 soapui 來執行服務。

我在這里做錯了什么?

請建議。

但是當我執行下面的代碼時,我在 Eclipse 控制台中得到了預期的結果

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(BookShelfService.class); //the service SEI
        factory.setAddress("http://localhost:8080/springapp/bookshelfservice");

        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());

        BookShelfService client = (BookShelfService) factory.create();

        BookVO reply = client.getBook("b1");
        System.out.println("Server said: " + reply.getAuthor());

我很確定您不需要創建自己的 cxf.xml,它已經由 cxf-core jar 提供。 所以我認為你不應該覆蓋它。 因為它會創建一個新的 Spring Bus,它可能沒有用 cxf 方式正確配置。 刪除該 cxf.xml 以避免沖突,只需在 bean.xml 中導入 cxf.xml 並將 cxf.xml 代碼放在 bean.xml 中。 試試這個更新的 bean.xml。

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:cxf="http://cxf.apache.org/core" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:jaxws="http://cxf.apache.org/jaxws" 
       xmlns:jaxrs="http://cxf.apache.org/jaxrs" 
       xsi:schemaLocation="http://cxf.apache.org/core 
                           http://cxf.apache.org/schemas/core.xsd
                           http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://cxf.apache.org/jaxws  
                           http://cxf.apache.org/schemas/jaxws.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" id="loggingInInterceptor" />
    <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" id="logOutInterceptor" />

    <cxf:bus>
        <cxf:ininterceptors>
            <ref bean="loggingInInterceptor" />
        </cxf:ininterceptors>
        <cxf:outinterceptors>
            <ref bean="logOutInterceptor" />
        </cxf:outinterceptors>
    </cxf:bus>


    <jaxws:endpoint id="bookShelfService" implementor="com.test.services.BookShelfServiceImpl" address="/bookshelfservice" />

</beans>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM