Javide
Javide

Reputation: 2657

How to expose both a SOAP web-service and RESTful API at the same time in Spring Boot?

In Spring Boot 1.4.3 I exposed a SOAP web service endpoint which works successfully on port 8080.

For the purpose of running a health check I also need to expose a RESTful API. I tried both using Actuator and a rest controller:

@RestController
public class RESTapis {

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/health")
    public String healthCheck() {
        return "ACK";
    }

}

but in both cases I get the same response: HTTP 405 (method not allowed).

The REST api returns HTTP 200 if I disable the web-service.

How can I have both the SOAP web-service and REST working at the same time?

Here is the web-service configuration:

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {

    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);

        return new ServletRegistrationBean(servlet, "/*");
    }
}

Upvotes: 3

Views: 4131

Answers (1)

pnyak_
pnyak_

Reputation: 359

So going off the messageDispatcherServlet method it looks like you are binding your servlet to all the incoming request due to the wildcard registration:

return new ServletRegistrationBean(servlet, "/*");

Hence the MessageDispatcher is intercepting all of your incoming requests and trying to find the /health and throwing http 405.

Fix:

return new ServletRegistrationBean(servlet, "/soap-api/*");

Explanation:

By binding the Message Dispatcher to a specific URI namespace we can ensure that all the request fired on the /soap-api/* namespace ONLY will be intercepted by the MessageDispatcher. And all the other requests will be intercepted by the DispatcherServlet making it possible to run a Rest interface in parallel to your Soap WS.

Suggestion:

Not knowing the reasons / specifics of the app, but going off the name of the method healthcheck(), you can look at using spring boot actuator to generate health checks, metrics for you app. You can also override it for customisations.

Reference for actuator: https://spring.io/guides/gs/actuator-service/

Upvotes: 8

Related Questions