參考版本
springboot 1.4.X <=========> cxf-spring-boot-starter-jaxws 3.1.X
springboot 1.5.X <=========> cxf-spring-boot-starter-jaxws 3.2.X
成功集成cxf后,發現只有webservice服務可以正常使用,其他請求url全部無法正常訪問。然后在cxf 配置文件中 WebServiceCxfCfg.java 更改此方法名:dispatcherServlet 改為 disServlet
//public ServletRegistrationBean dispatcherServlet() @Bean public ServletRegistrationBean disServlet(){ return new ServletRegistrationBean(new CXFServlet() , "/services/*"); }
即可成功訪問其他url
是因為 public ServletRegistrationBean dispatcherServlet() 把默認映射覆蓋掉了,把這個名字改掉,控制類方法就能訪問了。
整合的關鍵代碼
maven
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.2.6</version> </dependency>
CxfConfig
package com.xxx.xxx.common; import com.xxx.transfer.interceptor.IpAddressInInterceptor; import com.xxx.transfer.webservice.DepartmentService; import com.xxx.transfer.webservice.MedicalCardService; import com.xxx.transfer.webservice.impl.DepartmentServiceImpl; import com.xxx.transfer.webservice.impl.MedicalCardServiceImpl; import org.apache.cxf.Bus; import org.apache.cxf.bus.spring.SpringBus; import org.apache.cxf.jaxws.EndpointImpl; import org.apache.cxf.transport.servlet.CXFServlet; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.annotation.Resource; import javax.xml.ws.Endpoint; /** * @Description: CXF配置類 * @date 2019/10/1617:39 */ @Configuration public class CxfConfig { @Resource IpAddressInInterceptor ipAddressInInterceptor; /** * 成功集成cxf后,發現只有webservice服務可以正常使用,其他請求url全部無法正常訪問,是因為 public ServletRegistrationBean dispatcherServlet() 把默認映射覆蓋掉了,把這個名字改掉,控制類方法就能訪問了 * 修改dispatcherServlet為disServlet */ @Bean public ServletRegistrationBean disServlet() { return new ServletRegistrationBean(new CXFServlet(), "/webservice/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public MedicalCardService medicalCardService() { return new MedicalCardServiceImpl(); } @Bean public DepartmentService departmentService() { return new DepartmentServiceImpl(); } /** * 診療卡信息查詢 * * @return */ @Bean public Endpoint getPatInfo() { EndpointImpl endpoint = new EndpointImpl(springBus(), medicalCardService()); endpoint.getInInterceptors().add(ipAddressInInterceptor); endpoint.publish("/pat"); return endpoint; } /** * 獲取掛號科室 * * @return */ @Bean public Endpoint getDeptList() { EndpointImpl endpoint = new EndpointImpl(springBus(), departmentService()); //添加校驗攔截器 endpoint.getInInterceptors().add(ipAddressInInterceptor); endpoint.publish("/dept"); return endpoint; } }
service
package com.xxx.xxx.webservice; import javax.jws.WebParam; import javax.jws.WebService; /** * @Description: 獲取掛號科室 * @date 2019/10/1617:35 */ @WebService( // 暴露服務名稱 name = "getDeptList", // 命名空間,一般是接口的包名倒序 targetNamespace = "http://webservice.transfer.webservice.com" ) public interface DepartmentService { /** * 獲取掛號科室 */ public String getDeptList( // xxxx @WebParam(name = "xxx") String xxx, // xxx @WebParam(name = "xxx") String xxx, // xxx @WebParam(name = "xxx") String xxx, // xxx @WebParam(name = "xxx") String xxx ); }
package com.xxx.xxx.webservice.impl; import com.xxx.xxx.webservice.DepartmentService; import javax.jws.WebService; /** * @Description: * @date 2019/10/1617:38 */ @WebService( // 與接口中指定的name一致 serviceName = "getDeptList", // 與接口中的命名空間一致,一般是接口的包名倒 targetNamespace = "http://webservice.transfer.webservice.com", // 接口地址 endpointInterface = "com.xxx.transfer.webservice.DepartmentService" ) public class DepartmentServiceImpl implements DepartmentService { @Override public String getDeptList(String guahaofs, String riqi, String guahaobc, String guahaolb) { return "success"; } }