因为项目需要,需要再原先项目中发布或者调用WebService服务,考虑到集成Springboot框架,于是采用CXF框架。 WebService注册服务如下:
@Configuration public class CxfWebServiceConfig { // @Autowired // private Bus bus; // @Autowired private HelloWebService helloWebService; // @Bean // public ServletRegistrationBean dispatcherServlet() { // return new ServletRegistrationBean(new CXFServlet(), "/service/*");//发布服务名称 // } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } public HelloWebService helloWebService(){ return new HelloWebServiceImpl(); } @Bean public Endpoint endpoint(){ EndpointImpl endpoint = new EndpointImpl(springBus(),helloWebService); endpoint.publish("/HelloWebService"); return endpoint; } }在将CXF集成之后,WebService服务能够正常访问,但是原先的http服务不能访问 查阅资料后发现在springboot程序中默认注册的是 dispatcherServlet,在发不了WebService服务之后,相当于手动配置 ServletRegistrationBean,这时springboot不会再去注册默认的dispatcherServlet,解决办法就是需要我们在启动类里手动去注册一个dispatcherServlet
@Bean public ServletRegistrationBean restServlet() { //注解扫描上下文 AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); //项目包名 applicationContext.scan("com.eyedsion.his.web"); DispatcherServlet rest_dispatcherServlet = new DispatcherServlet(applicationContext); ServletRegistrationBean registrationBean = new ServletRegistrationBean(rest_dispatcherServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/*"); return registrationBean; }