2019-12-10 16:34:42 星期二
WebService是什么
WebService是一種跨編程語(yǔ)言和跨操作系統(tǒng)平臺(tái)的遠(yuǎn)程調(diào)用技術(shù),服務(wù)之間的相互調(diào)用與開(kāi)發(fā)語(yǔ)言無(wú)關(guān)
WebService平臺(tái)技術(shù)
- XML+XSD
WebService采用HTTP協(xié)議傳輸數(shù)據(jù),采用XML格式封裝數(shù)據(jù)
- SOAP
WebService通過(guò)HTTP協(xié)議發(fā)送請(qǐng)求和接收結(jié)果時(shí),發(fā)送的請(qǐng)求內(nèi)容和結(jié)果內(nèi)容都采用XML格式封裝,并增加了一些特定的HTTP消息頭,以說(shuō)明HTTP消息的內(nèi)容格式,這些特定的HTTP消息頭和XML內(nèi)容格式就是SOAP協(xié)議。SOAP提供了標(biāo)準(zhǔn)的RPC方法來(lái)調(diào)用Web Service。
- WSDL
基于XML的語(yǔ)言,用于描述Web Service及其函數(shù)、參數(shù)和返回值。它是WebService客戶端和服務(wù)器端都能理解的標(biāo)準(zhǔn)格式
springboot整合WebService
1、Springboot中已經(jīng)有配置的webservice的jar包,我們?cè)陂_(kāi)發(fā)時(shí)直接引入即可
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.6</version>
</dependency>
項(xiàng)目中的Springboot父版本采用2.2.0
2、webservice服務(wù)接口
@Service
@WebService(name = "MasterDataService", //該名字可自定義
targetNamespace = "http://webservice/system.biz.medical.com" // 該URL一般為當(dāng)前包名的倒序
)
public interface EmrWebService {
/**
* 服務(wù)調(diào)用
*
* @param data String
* @return String
*/
@WebMethod
String emrService(@WebParam String data);
}
3、接口實(shí)現(xiàn)
@Slf4j
@Service
@WebService(name = "MasterDataService", // 與接口中的name相同
targetNamespace = "http://webservice/system.biz.medical.com", // 一般為當(dāng)前包名的倒序
endpointInterface = "com.medical.biz.system.webservice.EmrWebService" // 為接口類的包名
)
public class EmrWebServiceImpl implements EmrWebService {
private static final String RESPONSE = "<Response><Header><SourceSystem>%s</SourceSystem><MessageID>%s</MessageID></Header><Body><ResultCode>%s</ResultCode><ResultContent>%s</ResultContent></Body></Response>";
@Override
public String emrService(@WebParam String data) {
log.info("接收參數(shù) => [ {} ]", data);
if (data.isEmpty()) {
return "傳入的參數(shù)為空";
}
return String.format(RESPONSE, "01", "", "0", "成功");
}
}
4、配置cxf服務(wù)發(fā)布
注意下方代碼中注釋信息中的坑點(diǎn)
@Configuration
public class CxfConfig {
private Bus bus;
private EmrWebService emrWebService;
@Autowired
public CxfConfig(Bus bus, EmrWebService emrWebService) {
this.bus = bus;
this.emrWebService = emrWebService;
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, emrWebService);
endpoint.publish("/MasterDataService");
return endpoint;
}
/**
* 坑點(diǎn):
* 1、方法名為dispatchServlet()
* 如果Springboot的主版本在2.0.X以下時(shí),可以正常啟動(dòng),此時(shí)在方法中配置的訪問(wèn)路徑將會(huì)覆蓋默認(rèn)或者在application.properties文件中配置server.servlet.context-path=中的值
* 如果Springboot的主版本在2.0.X以上時(shí),此時(shí)啟動(dòng)報(bào)錯(cuò),不能正常啟動(dòng)
* 此時(shí)需要將方法名更改,不能用dispatchServlet(),在方法中配置webservice的訪問(wèn)路徑,不會(huì)與項(xiàng)目配置的全局訪問(wèn)路徑?jīng)_突,
*
* @return ServletRegistrationBean
*/
@SuppressWarnings("all")
@Bean
public ServletRegistrationBean disServlet() {
// 此處配置的是webservice接口的訪問(wèn)地址,類似 http://127.0.0.1:8001/emr
return new ServletRegistrationBean(new CXFServlet(), "/emr/*");
}
}
5、啟動(dòng)項(xiàng)目,訪問(wèn)
http://127.0.0.1:8001/emr,可以看到服務(wù)信息
推薦使用soapUI進(jìn)行服務(wù)測(cè)試,結(jié)果如下:
