1.導(dǎo)入jar包
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.4.0</version>
</dependency>
2.新建接口
package com.das.consultation.webService.service;
import com.das.consultation.webService.config.SoapConst;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* created by jun on 2020/9/8
* describe:webservice接口
* version 1.0
*/
@WebService(targetNamespace = SoapConst.NAMESPACE_URI,name = "claSoap")
public interface ScheduleService {
@WebMethod
String HPS_getSchedule(@WebParam(name = "xmlStr") String xmlStr);
}
3.接口實現(xiàn)(具體接口實現(xiàn)根據(jù)自己的來)
package com.das.consultation.webService.service.Impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.das.consultation.entity.XmlMessage;
import com.das.consultation.entity.app.ScheduleInfo;
import com.das.consultation.util.JsonXmlUtils;
import com.das.consultation.util.PublicMethodUtil;
import com.das.consultation.webService.config.SoapConst;
import com.das.consultation.webService.service.ScheduleService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;
/**
* created by jun on 2020/9/11
* describe:
* version 1.0
*/
@WebService(
targetNamespace = SoapConst.NAMESPACE_URI, //wsdl命名空間
name = "claSoap", //portType名稱 客戶端生成代碼時 為接口名稱
serviceName = "claSoapSoapService", //服務(wù)name名稱
portName = "claSoapPortName", //port名稱
endpointInterface = "com.das.consultation.webService.service.ScheduleService")
public class ScheduleServiceImpl implements ScheduleService {
@Override
public String HPS_getSchedule(@RequestBody String xmlStr) {
JSONObject jsonObject = JsonXmlUtils.xmlToJson(xmlStr);
JSONObject request = jsonObject.getJSONObject("request");
JSONObject data = request.getJSONObject("data");
String orgCode = data.getString("orgcode");
String depId = data.getString("depid");
String doctorId = data.getString("doctorid");
String beginDate = data.getString("begindate");
String endDate = data.getString("enddate");
List<ScheduleInfo> scheduleInfos = new ArrayList<>();
ScheduleInfo scheduleInfo = new ScheduleInfo();
scheduleInfo.setScheduleid("213142494");
scheduleInfo.setDepid("12542076");
scheduleInfo.setDepname("眼科");
scheduleInfo.setDoctorid("235t32723115");
scheduleInfo.setDoctorname("曾醫(yī)生");
scheduleInfo.setVisitdate(PublicMethodUtil.ToDateTime("2020-8-12",4));
scheduleInfo.setIsyuyue("1");
scheduleInfo.setTimetype("白班");
scheduleInfo.setReglevel("普通門診");
String str = PublicMethodUtil.getStringDate();
scheduleInfo.setBegintime(PublicMethodUtil.strToDate(str));
scheduleInfo.setEndtime(PublicMethodUtil.strToDate(str));
scheduleInfo.setGuahaoamt((float) 23.21);
scheduleInfo.setVisitamt((float) 30.34);
scheduleInfo.setYuyuemax(214);
scheduleInfo.setYuyuenum(143);
scheduleInfos.add(scheduleInfo);
XmlMessage xmlMessage = new XmlMessage();
JSONArray array = null;
try {
array = (JSONArray) JSON.toJSON(scheduleInfos);
if (!StringUtils.isEmpty(orgCode) && !StringUtils.isEmpty(depId)&& !StringUtils.isEmpty(doctorId) && !StringUtils.isEmpty(beginDate) && !StringUtils.isEmpty(endDate)) {
if (array.size() != 0) {
xmlMessage.setResult("0");
xmlMessage.setDesc("查詢成功");
} else {
xmlMessage.setResult("1");
xmlMessage.setDesc("失敗");
return JsonXmlUtils.jsonToXml(null,xmlMessage);
}
} else {
xmlMessage.setResult("1");
xmlMessage.setDesc("參數(shù)錯誤");
return JsonXmlUtils.jsonToXml(null,xmlMessage);
}
} catch (Exception e) {
e.printStackTrace();
xmlMessage.setResult("1");
xmlMessage.setDesc("查詢異常");
return JsonXmlUtils.jsonToXml(null,xmlMessage);
}
return JsonXmlUtils.jsonArrayToXml(array,xmlMessage,"schedule",null,null);
}
}
4.發(fā)布webservice配置類
package com.das.consultation.webService.config;
import com.das.consultation.webService.service.Impl.ScheduleServiceImpl;
import com.das.consultation.webService.service.ScheduleService;
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.xml.ws.Endpoint;
/**
* created by jun on 2020/9/11
* describe:
* version 1.0
*/
@Configuration
public class CxfConfig {
@Bean("cxfServletRegistration")
public ServletRegistrationBean dispatcherServlet() {
//注冊servlet 攔截/NBA 開頭的請求 不設(shè)置 默認為:/services/*
return new ServletRegistrationBean(new CXFServlet(), "/cla/*");
}
/**
* 申明業(yè)務(wù)處理類 當然也可以直接 在實現(xiàn)類上標注 @Service
* @author ShiFan
*/
@Bean
public ScheduleService scheduleService() {
return new ScheduleServiceImpl();
}
/**
* 非必要項
*/
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
/**
* 發(fā)布endpoint
*/
@Bean
public Endpoint endpoint(ScheduleService scheduleService) {
EndpointImpl endpoint = new EndpointImpl(springBus(), scheduleService);
//發(fā)布地址
endpoint.publish("/claService");
return endpoint;
}
}
5.命名空間類
package com.das.consultation.webService.config;
/**
* created by jun on 2020/9/2
* describe:空間名
* version 1.0
*/
public class SoapConst {
public static final String NAMESPACE_URI = "http://service.webService.consultation.das.com";
}
訪問地址:[http://localhost:8083/cla/claService?wsdl
(http://localhost:8083/cla/claService?wsdl)出現(xiàn)以下畫面則成功發(fā)布:

1.png
如果運行出現(xiàn)以下錯誤:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-09-11 13:23:44.965 ERROR 28612 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
The Bean Validation API is on the classpath but no implementation could be found
Action:
Add an implementation, such as Hibernate Validator, to the classpath
Process finished with exit code 1
說明是springBoot版本和cxf版本不匹配比如我的版本是2.3.2對應(yīng)cxf版本應(yīng)是3.4.0如下:
springboot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
cxf版本
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.4.0</version>
</dependency>
服務(wù)端項目結(jié)構(gòu):

image1.png
6.測試cxf根據(jù)wsdl生成客戶端代碼
進入cxf安裝目錄如下:

2.png
進入cmd命令輸入如下命令則生成如上圖紅色框的客戶段代碼:
wsdl2java -encoding utf8 -p com.das.webservice.cxf -d D:\MyStudySpace\webservice\webserviceClient\src\main\java\com\das\webservice\cxf http://localhost:8083/cla/claService?wsdl
7.新建項目webserviceClient
拷貝生成的代碼到客戶端如下:

3.png
8.新建測試類ClientTest
package com.das.webservice.cxf;
/**
* created by jun on 2020/9/11
* describe:
* version 1.0
*/
public class ClientTest {
public static void main(String[] args) {
ClaSoap claSoap = new ClaSoapSoapService().getClaSoapPortName();
String result = claSoap.hpsGetSchedule("<request><head></head><data><orgcode>231434123122319645</orgcode><depid>123123</depid><doctorid>1435425998</doctorid><begindate>2018-10-3</begindate><enddate>2018-10-3</enddate></data></request>");
System.out.println(result);
}
}
運行代碼打印如下:
<?xml version="1.0" encoding="utf-8"?><request><head><result>0</result><desc>查詢成功</desc></head><data><schedule><isyuyue>1</isyuyue><visitdate>Wed Aug 12 00:00:00 CST 2020</visitdate><endtime>Fri Sep 11 15:14:05 CST 2020</endtime><visitamt>30.34</visitamt><begintime>Fri Sep 11 15:14:05 CST 2020</begintime><doctorname>曾醫(yī)生</doctorname><reglevel>普通門診</reglevel><guahaoamt>23.21</guahaoamt><doctorid>235t32723115</doctorid><yuyuemax>214</yuyuemax><depid>12542076</depid><timetype>白班</timetype><yuyuenum>143</yuyuenum><depname>眼科</depname><scheduleid>213142494</scheduleid></schedule></data></request>