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>


然后,添加[b]log4j.properties[/b]文件,放入你的classpath目录下
[quote]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
[/quote]

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

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

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

说明:添加了MessageTracing的log监控,这样发送和接收请求时都会输出相关log。

选择前文的某个test case,运行后,如下log:

[quote]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]
[/quote]

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

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

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

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

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

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

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

再次运行test case,server端的console输出如下:
[quote]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>[/quote]

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

相关代码依然在github上,branch为 Section_Request_Trace
[url]https://github.com/chenzhouce/spring-webservice-demo/blob/Section_Request_Trace/src/main/webapp/WEB-INF/spring-ws-servlet.xml[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值