Spring 引导:带有@RestController 的 Apache CXF SOAP

Spring Boot : Apache CXF SOAP with @RestController

我正在使用@RestController 制作Spring 启动休息服务,在同一个项目中我也公开了 Apache CXF SOAP 服务,如

@RestController代码

@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 服务在更改后 运行 正常,但 REST (@RestController) 停止工作,但如果我禁用这些方法

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

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

和运行 http://localhost:8007/mobileTopUpService/processTopUpRequest/ @RestController 运行很好但不是肥皂。

我需要运行 @RestController 和 CXF SOAP,请建议。

谢谢

我刚刚同时使用 SOAP 和 REST 服务。这是我的配置:(在答案的最后,我包含了一个示例项目)

application.properties

cxf.path=/services
cxf.servlet.load-on-startup=-1

WebServiceConfig

@Configuration
@ConditionalOnWebApplication
public class WebServiceConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(WsEndpointsConfiguration.class);


    @Autowired
    private Bus bus;


    @Value("${cxf.path}")
    private String cxfServletPath;


    @Autowired
    private YourServiceInterface yourService;


    public Logger getLOGGER() {
        return LOGGER;
    }


    public Bus getBus() {
        return bus;
    }


    public String getCxfServletPath() {
        return cxfServletPath;
    }


    public void setCxfServletPath(String cxfServletPath) {
        this.cxfServletPath = cxfServletPath;
    }


    public YourServiceInterface getYourServiceInterface() {
        return yourService;
    }


    @Bean
    public Endpoint yourWebServiceEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(getBus(), new YourWebServiceEndpoint(getYourServiceInterface()));
        endpoint.publish("/YourWebService");
        return endpoint;
    }


    @Bean
    public FilterRegistrationBean openEntityManagerInViewFilter() {
        FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
        filterRegBean.setFilter(new OpenEntityManagerInViewFilter());
        List<String> urlPatterns = new ArrayList<String>();
        urlPatterns.add(getCxfServletPath() + "/*");
        filterRegBean.setUrlPatterns(urlPatterns);
        if (getLOGGER().isDebugEnabled()) {
            getLOGGER().debug("Registering the 'OpenEntityManagerInViewFilter' filter for the '"
                .concat(getCxfServletPath() + "/*").concat("' URL."));
        }
        return filterRegBean;
    }
}

用您自己的服务接口替换@Autowired服务。

您可以在此处查看完整示例:

https://github.com/jcagarcia/proofs/tree/master/spring-security-and-formatters

上面提供的示例中的相关 类:

希望对您有所帮助,

我在 class 中将其解析为@EnableWebMvc,其中启动了启动应用程序 即 SpringApplication.run(ApplicationStartup.class, args);

也在 spring 引导 class 中移动了 ServletRegistrationBean,

禁用方法 @豆 public EmbeddedServletContainerFactory embeddedServletContainerFactory() {...}