最近项目需要在 SpringBoot 中集成 WebService,选择在 SpringBoot 中集成CXF。集成的配置类如下:
@Configuration public class CxfConfig { @Autowired private Bus bus; @Autowired UserService userService; /** * 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问 * 此方法被注释后:wsdl访问地址为https://127.0.0.1:8080/services/user?wsdl * 去掉注释后:wsdl访问地址为:https://127.0.0.1:8080/soap/user?wsdl * @return */ @SuppressWarnings("all") @Bean public ServletRegistrationBean dispatcherServlet() { return new ServletRegistrationBean(new CXFServlet(), "/soap/*"); } /** * JAX-WS 站点服务 */ @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(bus, userService); endpoint.publish("/user"); return endpoint; } }
项目在 application.properties 中定义的服务名称为“/server”。当我启动项目后,当试图去访问 /server 时返回 404 错误。排查问题排查了很久,自己的配置文件没有问题啊,最后将CXF的配置信息注释掉,就可以正常访问服务器了。
最终原因是下面一段代码导致:
@SuppressWarnings("all") @Bean public ServletRegistrationBean dispatcherServlet() { return new ServletRegistrationBean(new CXFServlet(), "/soap/*"); }
将 dispatcherServlet() 方法名称改为其他名称即可。