参考版本
1springboot 1.4.X <=========> cxf-spring-boot-starter-jaxws 3.1.X 2 3springboot 1.5.X <=========> cxf-spring-boot-starter-jaxws 3.2.X
成功集成cxf后,发现只有webservice服务可以正常使用,其他请求url全部无法正常访问。然后在cxf 配置文件中 WebServiceCxfCfg.java 更改此方法名:dispatcherServlet 改为 disServlet
1//public ServletRegistrationBean dispatcherServlet() 2 3@Bean 4public ServletRegistrationBean disServlet(){ 5 6 return new ServletRegistrationBean(new CXFServlet() , "/services/*"); 7}
即可成功访问其他url
是因为 public ServletRegistrationBean dispatcherServlet() 把默认映射覆盖掉了,把这个名字改掉,控制类方法就能访问了。
整合的关键代码
maven
1<dependency> 2 <groupId>org.apache.cxf</groupId> 3 <artifactId>cxf-rt-frontend-jaxws</artifactId> 4 <version>3.2.6</version> 5</dependency>
CxfConfig
1package com.xxx.xxx.common; 2 3import com.xxx.transfer.interceptor.IpAddressInInterceptor; 4import com.xxx.transfer.webservice.DepartmentService; 5import com.xxx.transfer.webservice.MedicalCardService; 6import com.xxx.transfer.webservice.impl.DepartmentServiceImpl; 7import com.xxx.transfer.webservice.impl.MedicalCardServiceImpl; 8import org.apache.cxf.Bus; 9import org.apache.cxf.bus.spring.SpringBus; 10import org.apache.cxf.jaxws.EndpointImpl; 11import org.apache.cxf.transport.servlet.CXFServlet; 12import org.springframework.boot.web.servlet.ServletRegistrationBean; 13import org.springframework.context.annotation.Bean; 14import org.springframework.context.annotation.Configuration; 15 16import javax.annotation.Resource; 17import javax.xml.ws.Endpoint; 18 19/** 20 * @Description: CXF配置类 21 * @date 2019/10/1617:39 22 */ 23@Configuration 24public class CxfConfig { 25 26 @Resource 27 IpAddressInInterceptor ipAddressInInterceptor; 28 29 /** 30 * 成功集成cxf后,发现只有webservice服务可以正常使用,其他请求url全部无法正常访问,是因为 public ServletRegistrationBean dispatcherServlet() 把默认映射覆盖掉了,把这个名字改掉,控制类方法就能访问了 31 * 修改dispatcherServlet为disServlet 32 */ 33 @Bean 34 public ServletRegistrationBean disServlet() { 35 return new ServletRegistrationBean(new CXFServlet(), "/webservice/*"); 36 } 37 38 @Bean(name = Bus.DEFAULT_BUS_ID) 39 public SpringBus springBus() { 40 return new SpringBus(); 41 } 42 43 @Bean 44 public MedicalCardService medicalCardService() { 45 return new MedicalCardServiceImpl(); 46 } 47 48 @Bean 49 public DepartmentService departmentService() { 50 return new DepartmentServiceImpl(); 51 } 52 53 /** 54 * 诊疗卡信息查询 55 * 56 * @return 57 */ 58 @Bean 59 public Endpoint getPatInfo() { 60 EndpointImpl endpoint = new EndpointImpl(springBus(), medicalCardService()); 61 endpoint.getInInterceptors().add(ipAddressInInterceptor); 62 endpoint.publish("/pat"); 63 return endpoint; 64 } 65 66 /** 67 * 获取挂号科室 68 * 69 * @return 70 */ 71 @Bean 72 public Endpoint getDeptList() { 73 EndpointImpl endpoint = new EndpointImpl(springBus(), departmentService()); 74 //添加校验拦截器 75 endpoint.getInInterceptors().add(ipAddressInInterceptor); 76 endpoint.publish("/dept"); 77 return endpoint; 78 } 79}
service
1package com.xxx.xxx.webservice; 2 3import javax.jws.WebParam; 4import javax.jws.WebService; 5 6/** 7 * @Description: 获取挂号科室 8 * @date 2019/10/1617:35 9 */ 10@WebService( 11 // 暴露服务名称 12 name = "getDeptList", 13 // 命名空间,一般是接口的包名倒序 14 targetNamespace = "http://webservice.transfer.webservice.com" 15) 16public interface DepartmentService { 17 18 /** 19 * 获取挂号科室 20 */ 21 public String getDeptList( 22 // xxxx 23 @WebParam(name = "xxx") String xxx, 24 // xxx 25 @WebParam(name = "xxx") String xxx, 26 // xxx 27 @WebParam(name = "xxx") String xxx, 28 // xxx 29 @WebParam(name = "xxx") String xxx 30 ); 31} 32 33package com.xxx.xxx.webservice.impl; 34 35import com.xxx.xxx.webservice.DepartmentService; 36 37import javax.jws.WebService; 38 39/** 40 * @Description: 41 * @date 2019/10/1617:38 42 */ 43@WebService( 44 // 与接口中指定的name一致 45 serviceName = "getDeptList", 46 // 与接口中的命名空间一致,一般是接口的包名倒 47 targetNamespace = "http://webservice.transfer.webservice.com", 48 // 接口地址 49 endpointInterface = "com.xxx.transfer.webservice.DepartmentService" 50) 51public class DepartmentServiceImpl implements DepartmentService { 52 @Override 53 public String getDeptList(String guahaofs, String riqi, String guahaobc, String guahaolb) { 54 return "success"; 55 } 56}