1.导入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.接口实现(具体接口实现根据自己的来)
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", //服务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("曾医生");
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("参数错误");
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.发布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 开头的请求 不设置 默认为:/services/*
return new ServletRegistrationBean(new CXFServlet(), "/cla/*");
}
/**
* 申明业务处理类 当然也可以直接 在实现类上标注 @Service
* @author ShiFan
*/
@Bean
public ScheduleService scheduleService() {
return new ScheduleServiceImpl();
}
/**
* 非必要项
*/
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
/**
* 发布endpoint
*/
@Bean
public Endpoint endpoint(ScheduleService scheduleService) {
EndpointImpl endpoint = new EndpointImpl(springBus(), scheduleService);
//发布地址
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)出现以下画面则成功发布:
如果运行出现以下错误:
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对应cxf版本应是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>
服务端项目结构:
image1.png6.测试cxf根据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.png8.新建测试类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>曾医生</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>
网友评论