Yes, there are reasons why you may want to add a web service endpoint to an existing Spring MVC app. The problem is that you will likely need to have a different path for each, which is fine.
You will need two servlets, a standard dispatcher servlet for handling HTTP/MVC and a MessageDispatcherServlet for handling SOAP calls.
The config can be tricky. First understand that you will have a dependency mismatch with Spring MVC when you add in the Spring-ws dependencies. You will need to exclude Spring-web as follows in your pom:
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<version>2.2.1.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
</exclusions>
</dependency>
Once you have taken care of that you will need to add in the two servlets, one to handle web requests through Spring MVC and one to handle SOAP.
I'm assuming no-xml config using Spring 4, SpringBoot is possible as well.
Here is the key code you will add to your web initializer:
DispatcherServlet servlet = new DispatcherServlet();
// no explicit configuration reference here: everything is configured in the root container for simplicity
servlet.setContextConfigLocation("");
/* TMT From Java EE 6 API Docs:
* Registers the given servlet instance with this ServletContext under the given servletName.
* The registered servlet may be further configured via the returned ServletRegistration object.
*/
ServletRegistration.Dynamic appServlet = servletContext.addServlet("appServlet", servlet);
appServlet.setLoadOnStartup(1);
appServlet.setAsyncSupported(true);
Set<String> mappingConflicts = appServlet.addMapping("/web/*");
MessageDispatcherServlet mds = new MessageDispatcherServlet();
mds.setTransformWsdlLocations(true);
mds.setApplicationContext(context);
mds.setTransformWsdlLocations(true);
ServletRegistration.Dynamic mdsServlet = servletContext.addServlet("mdsServlet", mds);
mdsServlet.addMapping("/wsep/*");
mdsServlet.setLoadOnStartup(2);
mdsServlet.setAsyncSupported(true);
That is really all there is to it. The rest of the config is standard stuff, found in any number of examples.
For instance, you can mix the spring IO examples for Spring MVC and Spring-WS easily as a test bed. Just make sure you set up the WebMvcConfigurerAdapter
and the WsConfigurerAdapter
accordingly. They will be two separate classes, annotated individually with @Configuration @EnableWebMvc
and @EnableWs @Configuration
respectively. They will have to be added to the component scan complete with your @Endpoint
classes.
Compile, run and test using a browser for the MVC stuff off the root context via /web/*
and the SOAP calls using SoapUI by importing the WSDL and hitting /wsep/*
off the root. Each path handled by each servlet.