Search code examples
soapspring-bootcxfspring-restcontroller

Spring Boot : Apache CXF SOAP with @RestController


I am making Spring Boot rest service using @RestController, in same project I am also exposing the Apache CXF SOAP service like

@RestController Code

@RestController
@RequestMapping(value = "/mobileTopUpService")
public class TopUpRestService {

@RequestMapping(value="/processTopUpRequest", method=RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)

    public ResponseEntity<TopUpRequestDTO> processTopUpRequest(HttpServletRequest httpServletRequest, @Valid RequestEntity<TopUpRequestDTO> _requestEntity) {


        return new ResponseEntity<>(new exampleDTO("hi"), HttpStatus.OK);
    }
}

Apache CXF SOAP

    @Configuration
    @Import(ApplicationConfiguration.class)
    public class WebServiceConfig
    {
        public static final String SERVLET_MAPPING_URL_PATH = "/*";
        public static final String SERVICE_NAME_URL_PATH = "/services";

        @Autowired
        private ApplicationConfiguration applicationConfiguration;

        @Bean
        public ServletRegistrationBean dispatcherServlet()
        {
            return new ServletRegistrationBean(new CXFServlet(), SERVLET_MAPPING_URL_PATH);
        }

        @Bean(name = Bus.DEFAULT_BUS_ID)
        public SpringBus springBus()
        {
            return new SpringBus();
        }

        @Bean
        public ERSBackendService ersBackendServiceImpl()
        {
            return new ERSBackendServiceImpl();
        }

        @Bean
        public Endpoint endpoint()
        {
            EndpointImpl endpoint = new EndpointImpl(springBus(), ersBackendServiceImpl());
            endpoint.publish(SERVICE_NAME_URL_PATH);

            AutomaticWorkQueue executorQueue = createThreadPoolExecutorQueue();
            endpoint.setExecutor(executorQueue);

            return endpoint;
        }
@Bean
public EmbeddedServletContainerFactory embeddedServletContainerFactory()
{
    TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory("/backend-service", Integer.valueOf(applicationConfiguration.getPort()));
    return factory;
}
}

SOAP Service are running fine after change but REST (@RestController) stop working, but if I disables the methods

//  @Bean
//  public ServletRegistrationBean dispatcherServlet()
//  {
//      return new ServletRegistrationBean(new CXFServlet(), SERVLET_MAPPING_URL_PATH);
//  }

and

    @Bean
//  public EmbeddedServletContainerFactory embeddedServletContainerFactory()
//  {
//      TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory("/backend-service", Integer.valueOf("8007"));
//      return factory;
//  }
//}

and run http://localhost:8007/mobileTopUpService/processTopUpRequest/ the @RestController runs fine but not soap.

I need to run both @RestController and CXF SOAP, kindly suggest.

thanks


Solution

  • I resolved it as @EnableWebMvc in class where starting boot app i.e SpringApplication.run(ApplicationStartup.class, args);

    Moved ServletRegistrationBean in spring boot class too,

    disbaled method @Bean public EmbeddedServletContainerFactory embeddedServletContainerFactory() {...}