Wrapping the invocation of an @Endpoint-annotated Spring-WS endpoint
(Java 6, Spring 3, Spring-WS开发者_如何学Go 2, JAXB2, Tomcat 6)
I have a requirement to log the request and response XML to a database table. I would like to be able to create a record on receipt of the request XML and update the same record when the response XML (or fault) is ready. Is there a way to do this with Spring-WS 2?
I want to wrap the invocation of the endpoint prior to unmarshalling the XML so I can save request XML, invoke the endpoint, and finally save response XML. I would use the unique ID returned from the DB after saving the request XML to identify the record that needs to be updated with the response XML.
All help is much appreciated!
You can use Spring-WS EndpointInterceptors.
Implement an interceptor.
You have to implement the org.springframework.ws.server.EndpointInterceptor
interface where methods to act on the request and response events are provided.
By accessing the MessageContext object, you can retrieve the request and response mesages (getRequest and getResponse methods). You can also use the setProperty method to store information in the request moment that is accessible in the response to correlate information (e.g., the ID of the record to update in the DB for example).
Configure the interceptor.
The interceptor either can be configured for a specific request or globally for all the web services. I copy some configuration from the documentation:
<sws:interceptors>
<bean class="samples.MyGlobalInterceptor"/>
<sws:payloadRoot namespaceUri="http://www.example.com">
<bean class="samples.MyPayloadRootInterceptor"/>
</sws:payloadRoot>
<sws:soapAction value="http://www.example.com/SoapAction">
<bean class="samples.MySoapActionInterceptor1"/>
<ref bean="mySoapActionInterceptor2"/>
</sws:soapAction>
</sws:interceptors>
<bean id="mySoapActionInterceptor2" class="samples.MySoapActionInterceptor2"/>
Yes you can log the soap request and response by copying the following line in your log4j.properties file.
log4j.rootCategory=INFO, 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
You can get more information from this link http://static.springsource.org/spring-ws/sites/2.0/reference/html/common.html
精彩评论