当我们在使用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
分享到:
相关推荐
Spring Web Service是Spring社区基于Spring提供的一个关注于创建”文档驱动”的Web Service的模块, 它的主要目标是方便基于”契约优先”(Contract-First)的SOAP服务的开发. 好像没有多少人讨论, 大多数的话题都是...
Spring Web Service 简单使用,配合JAX-B生成的Pojo,直接部署即可运行。
详细讲述了spring的基本内容,IOC、AOP、MVC、JDBC等等,由易到难,逐步深入,是初学spring不错的选择
第一章:Spring Web MVC入门 包括:是什么、能干什么、有什么、各个组成部分的功能、HelloWorld等 第二章:理解DispatcherServlet 包括:功能、配置、上下文关系、初始化顺序等 第三章:注解式控制器开发详解 ...
CXF Spring Web Service 程序,希望对大家有用!!
org.springframework.web.context.request.Log4jNestedDiagnosticContextInterceptor.class org.springframework.web.context.request.RequestAttributes.class org.springframework.web.context.request....
Spring Hibernate 简明教程 夏昕
关于Spring的69个面试问答——终极列表
简易版教程,可供新手学习参考使用,其中附有详细的代码说明和对应的案例。最后还有演示效果。
Spring Web Services 官网 Spring Web Services API。 Spring Web Services 开发文档。
《Spring Web Flow权威指南》介绍了Spring Web Flow的构建系统Spring Jumpstart、Spring Web Flow架构和基础知识,并结合示例讲述了Spring Web Flow的高级概念、流执行管理和驱动,同时说明了如何测试流。...
Spring2.5开发简明教程中文版 想学习Spring 的朋友一定要下哦!!
使用XFire+Spring构建Web Service使用XFire+Spring构建Web Service使用XFire+Spring构建Web Service使用XFire+Spring构建Web Service使用XFire+Spring构建Web Service使用XFire+Spring构建Web Service
NULL 博文链接:https://elim.iteye.com/blog/1983532
精通Spring4.0 企业应用开发实战——ISBN 978-7-121-30443-9 完整高清带目录。
Dwr2+Struts2+Spring2.5+Hibernate3实战——用户登录注册系统
最新版spring-web-5.2.3.RELEASE和spring-webmvc-5.2.3.RELEASE
Spring应用开发实战,光盘中附录B,包含Web Service WS的详细讲解。
原来的jax-ws不知道为什么总是不成功,最后放弃,换成这个。具体过程可以参考官网:http://docs.spring.io/spring-ws/site/reference/html/tutorial.html
spring web mvc 基础教程简单易学 适合入门