`
white_crucifix
  • 浏览: 95649 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring Web Service 简明教程(四)—— Log输出

阅读更多
    当我们在使用Spring Web Services进行开发调试阶段,一定会需要去跟踪请求和接收时的数据,以供我们确认web service接口是否正确,那么输出一些log是非常理所当然的手段。在sws的实现中已经对这些必要的log输出做了准备,而我们要做的就是将log4j配置上去。

    首先,以任何形式在项目中加入log4j的jar包,使用maven的话如下,或者自行添加都可以
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>        


    然后,添加log4j.properties文件,放入你的classpath目录下
引用
log4j.rootCategory=DEBUG, stdout

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


    说明:将log4j.rootCategory设置为DEBUG等级后,项目运行时就会输出大量debug信息,于是我run了一次tomcat试试,等待了一小会,发现没有输出任何debug信息,疑惑,然后在client端发送了一次请求,这时候才开始输出大量的log信息,原来sws的初始化行为会在第一次访问时进行,而不会在server启动时直接初始化。

    然后,我不需要查看那么多的debug信息,所以将log4j.rootCategory设置为Info,stdout,将debug信息关闭。

    接着,为了查看发送和接收请求的log,需要添加一行配置在rootCategory下方,如下
引用
log4j.logger.org.springframework.ws.server.MessageTracing=DEBUG


    说明:添加了MessageTracing的log监控,这样发送和接收请求时都会输出相关log。
   
    选择前文的某个test case,运行后,如下log:

引用
DEBUG [server.MessageTracing.received] Received request [SaajSoapMessage {http://mycompany.com/hr/schemas}user]
DEBUG [server.MessageTracing.sent] Sent response [SaajSoapMessage {http://mycompany.com/hr/schemas}user] for request [SaajSoapMessage {http://mycompany.com/hr/schemas}user]


    第一次看到结果时,让我很失望,信息量太少,几乎没有太大价值,于是默默的将这行配置注释了。

    所以,只能继续阅读官方文档,最后终于被我看到了能够优雅的收集输入输出数据的手段,而这手段就来自于Interceptor拦截器的使用。

    注意:拦截器需要依赖commons-log的jar包,若是用maven配置了spring-ws-core,应该会自动导入commons-log,无需手动再添加到pom.xml。如果按照普通方式构建的项目,则需自行添加。

    拦截器的含义应该都能明白,就是可以在某个业务逻辑之前或者之后提供额外的行为,比如记录接收的数据和返回的数据。而且就这个需求,spring已经给我们做好了现成的拦截器,不需要自己写了,只需要配置就可以。

    在/WEB-INF/spring-ws-servlet.xml中,添加一段配置:
<sws:interceptors>
<bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>
</sws:interceptors>


   说明:这是spring提供的对Payload方式(即我们前文使用的web service方式)的拦截器,还有另一个SoapEnvelopeLoggingInterceptor这里就不举例了。

   同时,拦截器中是通过log debug形式输出,所以在前文log4j.properties中要加入对这个Interceptor的监控。
引用
log4j.logger.org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor=DEBUG


   再次运行test case,server端的console输出如下:
引用
DEBUG [endpoint.interceptor.PayloadLoggingInterceptor] Request: <ns2:user xmlns:ns2="http://mycompany.com/hr/schemas"><ns2:id>1</ns2:id><ns2:name>chenzhouce</ns2:name></ns2:user>
DEBUG [endpoint.interceptor.PayloadLoggingInterceptor] Response: <ns2:user xmlns:ns2="http://mycompany.com/hr/schemas"><ns2:id>2</ns2:id><ns2:name>Hello Spring!</ns2:name></ns2:user>

   
    说明: request就是server端接收的数据,response是endpoint处理过后返回给client端的数据。完整的数据显示,除了没有换行看起来会累点。

    相关代码依然在github上,branch为 Section_Request_Trace
https://github.com/chenzhouce/spring-webservice-demo/blob/Section_Request_Trace/src/main/webapp/WEB-INF/spring-ws-servlet.xml
   
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics