3 - Reference Documentation
3 - Reference Documentation
written
to a DOM tree or buffer.
For more information about full streaming, refer to the class-level Javadoc for
StreamingWebServiceMessage and
StreamingPayload .
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<property name="soapVersion">
<util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12"/>
</property>
</bean>
</beans>
Caution
Even though both versions of SOAP are quite similar in format, the 1.2
version is not backwards
compatible with 1.1 because it uses a different XML namespace.
Other major differences between
SOAP 1.1 and 1.2 include the different structure
of a Fault, and the fact that SOAPAction HTTP
headers are effectively
deprecated, thought they still work.
One important thing to note with SOAP version numbers, or WS-* specification
version numbers in
general, is that the latest version of a specification is
generally not the most popular version.
For SOAP,
this means that currently, the best version to use is 1.1.
Version 1.2 might become more popular in the
future, but currently 1.1 is the safest bet.
4.1.4 MessageContext
Typically, messages come in pairs: a request and a response. A request is created on the client-side,
which is sent over some
transport to the server-side, where a response is generated. This response gets
sent back to the client, where it is read.
4.2 TransportContext
One of the key properties of the SOAP protocol is that it tries to be transport-agnostic. This is why, for
instance, Spring-WS does
not support mapping messages to endpoints by HTTP request URL, but
rather by mesage content.
https://docs.spring.io/spring-ws/docs/2.4.0.RELEASE/reference/htmlsingle/ 20/71
09/03/2022 08:45 Spring Web Services Reference Documentation
However, sometimes it is necessary to get access to the underlying transport, either on the client or server
side. For this, Spring
Web Services has the TransportContext . The transport
context allows access to the underlying WebServiceConnection ,
which typically
is a HttpServletConnection on the server side; or a
HttpUrlConnection or CommonsHttpConnection on
the client side.
For example, you can obtain the IP address of the current request in a server-side endpoint or
interceptor like so:
XPath is a fourth generation declarative language that allows you to specify which nodes you want
to
process without specifying exactly how the processor is supposed to navigate to those nodes.
XPath's
data model is very well designed to support exactly what almost all developers want from
XML. For
instance, it merges all adjacent text including that in CDATA sections, allows values to be
calculated that skip over comments and processing instructions` and include text from child and
descendant elements, and requires all external entity references to be resolved. In practice, XPath
expressions tend to be much more robust against unexpected but perhaps insignificant changes in
the
input document.
Spring Web Services has two ways to use XPath within your application: the faster
XPathExpression or the more flexible
XPathTemplate .
4.3.1 XPathExpression
The XPathExpression is an abstraction over a compiled XPath expression,
such as the Java 5
javax.xml.xpath.XPathExpression , or the Jaxen
XPath class.
To construct an expression in an application context, there is
the
XPathExpressionFactoryBean . Here is an example which uses this factory bean:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
</bean>
<constructor-arg ref="nameExpression"/>
</bean>
</beans>
The expression above does not use namespaces, but we could set those using the
namespaces property of the factory bean.
The expression
can be used in the code as follows:
package sample;
this.nameExpression = nameExpression;
https://docs.spring.io/spring-ws/docs/2.4.0.RELEASE/reference/htmlsingle/ 21/71
09/03/2022 08:45 Spring Web Services Reference Documentation
p p
}
For a more flexible approach, you can use a NodeMapper , which is similar
to the RowMapper in Spring's JDBC support. The
following
example shows how we can use it:
package sample;
this.contactExpression = contactExpression;
new NodeMapper() {
});
4.3.2 XPathTemplate
The XPathExpression only allows you to evaluate a single, pre-compiled
expression. A more flexible, though slower,
alternative is the XpathTemplate .
This class follows the common template pattern used throughout Spring (JdbcTemplate,
JmsTemplate, etc.).
Here is an example:
package sample;
https://docs.spring.io/spring-ws/docs/2.4.0.RELEASE/reference/htmlsingle/ 22/71
09/03/2022 08:45 Spring Web Services Reference Documentation
Caution
Make sure to use Commons Logging version 1.1 or higher. Earlier versions have class loading issues,
and
do not integrate with the Log4J TRACE level.
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
5.1 Introduction
Spring-WS's server-side support is designed around a
MessageDispatcher that dispatches incoming
messages to endpoints,
with configurable endpoint mappings, response
generation, and endpoint interception.
Endpoints are typically annotated with the
@Endpoint annotation, and have
one or more handling methods.
These methods handle incoming XML request messages by
inspecting parts of the message (typically the
payload), and create some sort of response.
You annotate the method with another
annotation, typically @PayloadRoot ,
to indicate what sort of messages it can handle.
5.2 The MessageDispatcher
The server-side of Spring-WS is designed around a central class that dispatches incoming XML messages to
endpoints. Spring-
WS's MessageDispatcher is extremely flexible, allowing you to
use any sort of class as an endpoint, as long as it can be
configured in the Spring IoC container.
In a way, the message dispatcher resembles Spring's DispatcherServlet , the
“Front
Controller” used in Spring Web MVC.
https://docs.spring.io/spring-ws/docs/2.4.0.RELEASE/reference/htmlsingle/ 23/71
09/03/2022 08:45 Spring Web Services Reference Documentation
When a MessageDispatcher is set up for use and a request comes in for that
specific dispatcher, said MessageDispatcher
starts processing the request. The
list below describes the complete process a request goes through when handled by a
MessageDispatcher :
Exceptions that are thrown during handling of the request get picked up by any of the endpoint exception
resolvers that are
declared in the application context. Using these exception resolvers allows you to define
custom behaviors (such as returning a
SOAP Fault) in case such exceptions get thrown.
5.3 Transports
Spring Web Services supports multiple transport protocols. The most common is the HTTP transport, for which
a custom servlet
is supplied, but it is also possible to send messages over JMS, and even email.
5.3.1 MessageDispatcherServlet
The MessageDispatcherServlet is a standard Servlet
which
conveniently extends from the standard Spring Web
DispatcherServlet , and wraps
a MessageDispatcher . As such, it combines the attributes of these into one:
as a
MessageDispatcher , it follows the same request handling flow as described
in the previous section.
As a servlet, the
MessageDispatcherServlet is configured in the web.xml of
your web application. Requests that you want the
MessageDispatcherServlet to
handle will have to be mapped using a URL mapping in the same web.xml file. This is
standard Java EE servlet configuration; an example of such a
MessageDispatcherServlet declaration and mapping can be
found below.
<web-app>
<servlet>
<servlet-name>spring-ws</servlet-name>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-ws</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
extends AbstractAnnotationConfigMessageDispatcherServletInitializer {
@Override
@Override
In the example above, we tell Spring that endpoint bean definitions can be found in the MyEndpointConfig
class (which is a
@Configuration class).
Other bean definitions (typically services, repositories, etc.) can be found in the MyRootConfig
class.
By default, the AbstractAnnotationConfigMessageDispatcherServletInitializer maps the servlet to
two patterns:
/services and *.wsdl , though this can be changed by overriding the
getServletMappings() method.
For more details on
the programmatic configuration of the MessageDispatcherServlet , refer to the
Javadoc of
AbstractMessageDispatcherServletInitializer and
AbstractAnnotationConfigMessageDispatcherServletInitializer .
@Bean
The WSDL defined in the ' orders.wsdl ' file on the classpath can then be accessed via
GET requests to a URL of the following
form (substitute the host, port and
servlet context path as appropriate).
http://localhost:8080/spring-ws/orders.wsdl
<web-app>
<servlet>
<servlet-name>spring-ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-ws</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
<sws:dynamic-wsdl id="orders"
portTypeName="Orders"
locationUri="http://localhost:8080/ordersService/">
<sws:xsd location="Orders.xsd"/>
</sws:dynamic-wsdl>
@Bean
definition.setPortTypeName("Orders");
definition.setLocationUri("http://localhost:8080/ordersService/");
https://docs.spring.io/spring-ws/docs/2.4.0.RELEASE/reference/htmlsingle/ 26/71
09/03/2022 08:45 Spring Web Services Reference Documentation
return definition;
If you want to use multiple schemas, either by includes or imports, you will want to
put Commons XMLSchema on the class path.
If Commons XMLSchema is on the class path, the above <dynamic-wsdl>
element will follow all XSD imports and includes,
and will inline them in the WSDL as a single XSD.
This greatly simplifies the deployment of the schemas, which still making it
possible to edit them
separately.
Caution
Even though it can be quite handy to create the WSDL at runtime from your XSDs, there
are a couple of
drawbacks to this approach. First off, though we try to keep the WSDL generation
process consistent
between releases, there is still the possibility that it changes (slightly).
Second, the generation is a bit
slow, though once generated, the WSDL is cached for later
reference.
<beans>
<bean class="org.springframework.ws.transport.http.WebServiceMessageReceiverHandlerAdapter"/>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
</bean
...
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
</beans>
https://docs.spring.io/spring-ws/docs/2.4.0.RELEASE/reference/htmlsingle/ 27/71
09/03/2022 08:45 Spring Web Services Reference Documentation
<beans>
<bean class="org.springframework.ws.transport.http.WebServiceMessageReceiverHandlerAdapter"/>
<bean class="org.springframework.ws.transport.http.WsdlDefinitionHandlerAdapter"/>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="*.wsdl">myServiceDefinition</prop>
</props>
</property>
</bean>
</bean>
...
</beans>
5.3.3 JMS transport
Spring Web Services supports server-side JMS handling through the JMS functionality provided in the
Spring framework. Spring
Web Services provides the WebServiceMessageListener
to plug in to a MessageListenerContainer . This message listener
requires a
WebServiceMessageFactory to and
MessageDispatcher to operate. The following piece of configuration
shows
this:
<beans>
</bean>
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="messageListener">
<bean class="org.springframework.ws.transport.jms.WebServiceMessageListener">
</bean>
</property>
</bean>
<property name="endpointMappings">
<bean
class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping
<property name="defaultEndpoint">
<bean class="com.example.MyEndpoint"/>
https://docs.spring.io/spring-ws/docs/2.4.0.RELEASE/reference/htmlsingle/ 28/71
09/03/2022 08:45 Spring Web Services Reference Documentation
</property>
</bean>
</property>
</bean>
</beans>
5.3.4 Email transport
In addition to HTTP and JMS, Spring Web Services also provides server-side email handling. This
functionality is provided
through the MailMessageReceiver class. This class
monitors a POP3 or IMAP folder, converts the email to a
WebServiceMessage ,
sends any response using SMTP. The host names can be configured through the
storeUri, which
indicates the mail folder to monitor for requests (typically a POP3 or IMAP folder),
and a transportUri, which indicates the server
to use for sending responses (typically a SMTP server).
As an alternative to the polling approaches, which are quite inefficient, there is a monitoring strategy
that uses IMAP IDLE. The
IDLE command is an optional
expansion of the IMAP email protocol that allows the mail server to send new message updates to
the
MailMessageReceiver asynchronously. If you use a IMAP server that supports the
IDLE command, you can plug in the
ImapIdleMonitoringStrategy
into the monitoringStrategy property.
In addition to a supporting server, you will need to use
JavaMail version 1.4.1 or higher.
The following piece of configuration shows how to use the server-side email support, overiding the
default polling interval to a
value which checks every 30 seconds
(30.000 milliseconds):
<beans>
<property name="monitoringStrategy">
<bean class="org.springframework.ws.transport.mail.monitor.PollingMonitoringStrategy">
</bean>
</property>
</bean>
<property name="endpointMappings">
<bean
class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping
<property name="defaultEndpoint">
<bean class="com.example.MyEndpoint"/>
</property>
</bean>
</property>
</bean>
</beans>
https://docs.spring.io/spring-ws/docs/2.4.0.RELEASE/reference/htmlsingle/ 29/71
09/03/2022 08:45 Spring Web Services Reference Documentation
When using the embedded HTTP server, no external deployment descriptor is needed
( web.xml ).
You only need to define an
instance of the server and configure it to handle incoming requests.
The remoting module in the Core Spring Framework
contains a convenient factory bean for the HTTP server:
the SimpleHttpServerFactoryBean .
The most important property is
contexts, which maps context paths to corresponding
HttpHandler s.
To draw parallels with the servlet world, the contexts property plays
the role of servlet mappings in web.xml and the
WebServiceMessageReceiverHttpHandler is the equivalent of
a MessageDispatcherServlet .
The following snippet shows a simple configuration example of the HTTP server
transport:
<beans>
</bean>
</bean>
<property name="contexts">
<map>
</map>
</property>
</bean>
</bean>
</bean>
</beans>
5.3.6 XMPP transport
Finally, Spring Web Services 2.0 introduced support for XMPP, otherwise known as Jabber. The support
is based on the Smack
library.
Spring Web Services support for XMPP is very similar to the other transports: there is a a
XmppMessageSender for the
WebServiceTemplate and
and a XmppMessageReceiver to use with the
MessageDispatcher .
The following example shows how to set up the server-side XMPP components:
https://docs.spring.io/spring-ws/docs/2.4.0.RELEASE/reference/htmlsingle/ 30/71