寫在前面:
由于在當(dāng)前項(xiàng)目中需要基于springboot實(shí)現(xiàn)webservice服務(wù)發(fā)布,甲方推薦使用cxf模式,但網(wǎng)絡(luò)上的一些教程很多都相對(duì)復(fù)雜,此處力爭使用通俗易懂的方式實(shí)現(xiàn)基于cxf的webservice服務(wù)發(fā)布。
發(fā)布期間遇到的一個(gè)大坑是集成了cxf以后,工程里面原有的http請(qǐng)求失效了,在啟動(dòng)類重寫了dispatcherServlet()以后,發(fā)現(xiàn)http請(qǐng)求中的所有FormData數(shù)據(jù)都無法傳入,這是因?yàn)閟pringboot自動(dòng)注冊(cè)的ServletRegistrationBean默認(rèn)名稱就為dispatcherServlet(),重寫以后,會(huì)使得原有的請(qǐng)求失效,只保留了webservice的請(qǐng)求,因此中級(jí)解決方案就是將webservice配置中的ServletRegistrationBean改名,使其與springboot自動(dòng)注冊(cè)的名稱不一致即可實(shí)現(xiàn)webservice請(qǐng)求與http請(qǐng)求共存!
1. 實(shí)現(xiàn)思路

2. 具體實(shí)現(xiàn)
2.1 maven依賴
為springboot集成cxf添加必要的maven依賴
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.11</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-soap</artifactId>
<version>3.1.11</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.11</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.1.11</version>
</dependency>
2.2 編寫CxfConfig.java
import com.csg.service.*;
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.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
/**
* @Author
* @Date: 2019/9/19 15:04
* @Description:
**/
@Configuration
public class CxfConfig {
//默認(rèn)servlet路徑/*,如果覆寫則按照自己定義的來
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Autowired
private Bus bus;
@Autowired
UserWebService userWebService;
@Autowired
OrgWebService OrgService;
@Autowired
RoleWebServiceroleWebService;
@Autowired
UserRoleRelationWebService userRoleRelationWebService;
@Bean
public Endpoint endpointIscUserWebService() {
EndpointImpl endpoint = new EndpointImpl(bus,userWebService);
endpoint.publish("/WebServiceSyncUser");//接口發(fā)布在 /UserWebService 目錄下
return endpoint;
}
@Bean
public Endpoint endpointIscBizOrgService() {
EndpointImpl endpoint = new EndpointImpl(bus,orgService);
endpoint.publish("/WebServiceSyncBizOrg");//接口發(fā)布在 /BizOrgService 目錄下
return endpoint;
}
@Bean
public Endpoint endpointIscRoleService() {
EndpointImpl endpoint = new EndpointImpl(bus,roleWebService);
endpoint.publish("/WebServiceSyncBizRole");//接口發(fā)布在 /RoleService 目錄下
return endpoint;
}
@Bean
public Endpoint endpointIscUserRoleRelationWebService() {
EndpointImpl endpoint = new EndpointImpl(bus,userRoleRelationWebService);
endpoint.publish("/WebServiceSyncUserBizRoleRelation");//接口發(fā)布在 /UserRoleRelationWebService 目錄下
return endpoint;
}
}
2.3 編寫功能代碼
此處以UserWebService 為例:
- UserWebService 接口部分
@WebService
public interface UserWebService {
@WebMethod
boolean syncUserRequest(SyncUserPack syncPack);
}
- UserWebServiceImpl具體實(shí)現(xiàn)部分
//name暴露的服務(wù)名稱, targetNamespace:命名空間,設(shè)置為接口的包名倒寫(默認(rèn)是本類包名倒寫). endpointInterface接口地址
@WebService(serviceName = "WebServiceSyncUser" ,targetNamespace ="http://service.csg.com/" ,endpointInterface = "com.csg.service.UserWebService")
@Component
public class UserWebServiceImpl extends BaseServiceImpl implements UserWebService {
@Override
public boolean syncUserRequest(SyncUserPack syncPack) {
UserEntity userEntity = syncPack.getEntities();
if (iscUserEntity.equals("") || iscUserEntity == null) {
throw new RRException("操作對(duì)象為空!");
}
String operate = userEntity.getOperate();
boolean operateResult = false;
switch (operate) {
case "ADD":
System.out.println("test-add");
break;
case "UPD":
System.out.println("test-update");
break;
case "DEL":
System.out.println("test-delete");
break;
default:
System.out.println("此次操作在基本操作之外,請(qǐng)修改后重新操作!");
break;
}
return operateResult;
}
}
2.4 發(fā)布成功
直接訪問:http://127.0.0.1:8080/services就能看到自己發(fā)布的webservice接口了
3. 踩坑記錄:關(guān)于CxfConfig中的dispatcherServlet()
一開始是直接從github抄的CxfConfig,看著網(wǎng)絡(luò)上大多也是寫這個(gè),就無腦copy,誰知這個(gè)是個(gè)大坑。
CxfConfig.java中的dispatcherServlet
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/*");
}
直接使用dispatcherServlet導(dǎo)致的問題第一個(gè)是,http請(qǐng)求失效,只有webservice請(qǐng)求能用。原因在前方已寫,此處不再贅述。
經(jīng)過多方百度以后,決定重寫dispatcherServlet,于是乎,就在啟動(dòng)類底下多了這么個(gè)Bean:
package com.csg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
@SpringBootApplication
@EnableScheduling
public class CsgApplication {
public static void main(String[] args) {
SpringApplication.run(CorsApplication.class, args);
}
/**
* 重寫ServletRegistrationBean,解決集成cxf以后,http請(qǐng)求失效情況
* @return
*/
@Bean
public ServletRegistrationBean restServlet() {
//注解掃描上下文
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
//項(xiàng)目包名
applicationContext.scan("com.csg");
DispatcherServlet rest_dispatcherServlet = new DispatcherServlet(applicationContext);
ServletRegistrationBean registrationBean = new ServletRegistrationBean(rest_dispatcherServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/*");
return registrationBean;
}
}
重寫了這個(gè)bean以后,http請(qǐng)求能用了,webservice請(qǐng)求也能用了!?。¢_心~但是?。。。。。。。?!經(jīng)過多方測試,發(fā)現(xiàn)所有的FormData請(qǐng)求,參數(shù)都傳不進(jìn)去!參數(shù)全報(bào)空……
測試出結(jié)果的我已經(jīng)不是茫然能描述的了……

后來后來,經(jīng)過多方查找以后,終于揪出了罪魁禍?zhǔn)祝【褪撬?br> CxfConfig.java中的dispatcherServlet
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/*");
}
因?yàn)楹蛃pringboot自動(dòng)注冊(cè)重名了?。?!
所以,終極解決方案就是——改名字!

把CxfConfig.java中的dispatcherServlet改成dispatcher,解決了!撒花兒!
@Bean
public ServletRegistrationBean dispatcher() {
return new ServletRegistrationBean(new CXFServlet(), "/*");
}