目录
- 1. 概述
- 2. 实现步骤
- 2.1 POM现加依赖
- 2.2 定义接口 IWebService
- 2.3 创建类 IWebServiceImpl 并实现接口 IWebService
- 2.4 配置类 CxfConfig
- 2.5 启动服务
- 2.6 测试
开发环境: IDEA 2022.1.4
1. 概述
虽然webservice这块使用很少,但在局域网作服务还是相当不错。今天突生想法,想做一个来试试,查阅百度资料,亲手实践下。方便操作,便在现有得SpringBoot项目上,来实践下,也顺便试试Spring web服务与webservice功能是否可以一起发布。
我主要添加了三个java文件,一个接口IWebService,一个接口实现类IWebServiceImpl,一个配置类CxfConfig。
2. 实现步骤
2.1 POM现加依赖
<!-- WebSevice -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
2.2 定义接口 IWebService
@WebService(name = "WebService", targetNamespace = "service.xtjk.ceaning.com")
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface IWebService {
@WebMethod
String BedInfo() throws JsonProcessingException;
@WebMethod
Result<?> DeptInfo();
@WebMethod
Result<?> UserInfo();
@WebMethod
Result<?> WardInfo();
@WebMethod
Result<?> OrderInfo(@WebParam(name = "param") String param);
@WebMethod
Result<?> InPatiengInfo(@WebParam(name = "param") String param);
@WebMethod
Result<?> SaveGlu(@WebParam(name = "param") String param);
}
2.3 创建类 IWebServiceImpl 并实现接口 IWebService
@Slf4j
@Component
@WebService(name = "WebService",
targetNamespace = "service.xtjk.ceaning.com",
endpointInterface = "com.ceaning.xtjk.service.IWebService")
public class IWebServiceImpl implements IWebService {
@Autowired
private DictionaryMapper dictionaryMapper;
@Autowired
private OrderMapper orderMapper;
@Autowired
private InPatientMapper inPatientMapper;
@Autowired
private ResultMapper resultMapper;
@Override
public String BedInfo() throws JsonProcessingException {
ObjectMapper obj= new ObjectMapper();
List<Bed> list= null;
try{
list= dictionaryMapper.getBedInfo();
return obj.writeValueAsString(Result.ok(list));
} catch (Exception e){
log.error(e.getMessage());
return obj.writeValueAsString(Result.exception(e.getMessage()));
}
}
@Override
public String DeptInfo() throws JsonProcessingException {
ObjectMapper obj= new ObjectMapper();
List<Dept> list= null;
try{
list= dictionaryMapper.getDeptInfo();
return obj.writeValueAsString(Result.ok(list));
} catch (Exception e){
log.error(e.getMessage());
return obj.writeValueAsString(Result.exception(e.getMessage()));
}
}
@Override
public String UserInfo() throws JsonProcessingException {
ObjectMapper obj= new ObjectMapper();
List<User> list= null;
try{
list= dictionaryMapper.getUserInfo();
return obj.writeValueAsString(Result.ok(list));
} catch (Exception e){
log.error(e.getMessage());
return obj.writeValueAsString(Result.exception(e.getMessage()));
}
}
@Override
public String WardInfo() throws JsonProcessingException {
ObjectMapper obj= new ObjectMapper();
List<Ward> list= null;
try{
list= dictionaryMapper.getWardInfo();
return obj.writeValueAsString(Result.ok(list));
} catch (Exception e){
log.error(e.getMessage());
return obj.writeValueAsString(Result.exception(e.getMessage()));
}
}
@Override
public String OrderInfo(String param) throws JsonProcessingException {
ObjectMapper mapper= new ObjectMapper();
List<Order> list= null;
try{
OrderParam obj= mapper.readValue(param, OrderParam.class);
list= orderMapper.getList(obj);
return mapper.writeValueAsString(Result.ok(list));
} catch (Exception e){
log.error(e.getMessage());
return mapper.writeValueAsString(Result.exception(e.getMessage()));
}
}
@Override
public String InPatiengInfo(String param) throws JsonProcessingException {
ObjectMapper mapper= new ObjectMapper();
List<InPatient> list= null;
try{
InPatientParam obj= mapper.readValue(param, InPatientParam.class);
list= inPatientMapper.getList(obj);
return mapper.writeValueAsString(Result.ok(list));
} catch (Exception e){
log.error(e.getMessage());
return mapper.writeValueAsString(Result.exception(e.getMessage()));
}
}
@Override
public String SaveGlu(String param) throws JsonProcessingException {
ObjectMapper mapper= new ObjectMapper();
DbResult ret= null;
try{
ResultParam obj= mapper.readValue(param, ResultParam.class);
ret= resultMapper.save(obj);
if (ret.getCode().trim().equals("T")){
return mapper.writeValueAsString(Result.ok(ret.getMessage()));
}
else{
return mapper.writeValueAsString(Result.error(ret.getMessage()));
}
} catch (Exception e){
log.error(e.getMessage());
return mapper.writeValueAsString(Result.exception(e.getMessage()));
}
}
}
2.4 配置类 CxfConfig
@Configuration
public class CxfConfig {
@Autowired
private IWebService service;
@Bean
public ServletRegistrationBean disServlet(){
return new ServletRegistrationBean(new CXFServlet(), "/open/xtjk/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus(){
return new SpringBus();
}
@Bean
public Endpoint endpoint(){
EndpointImpl endpoint= new EndpointImpl(springBus(), service);
endpoint.publish("/IWebService");
return endpoint;
}
}
2.5 启动服务
浏览器访问: http://127.0.0.1:8090/open/xtjk/IWebService?wsdl
需要注意URL路径和CxfConfig得配置关联
2.6 测试
我使用另一个电脑安装得SoapUI进行测试,数据获取正常。就不截图了。
篇幅有限,就不罗列其他得了。Springboot相关知识网上很多。