SpringBoot创建WebService方法详解
作者:ceaningking 发布时间:2022-02-08 10:29:31
标签:SpringBoot,WebService
开发环境: 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相关知识网上很多。
来源:https://blog.csdn.net/ceaningking/article/details/130456201
0
投稿
猜你喜欢
- File类简介package com.file;import java.io.File;import java.io.IOException
- 测试Spring Boot定时任务冲突时,使用的线程数量引入依赖:Spring Boot 2.6.1 <dependency>
- 这个很基础的知识,但我至今才意识到它。想想也很失败。直接上代码:很简单public class Base
- 官网教程一、翻转(镜像)头文件 quick_opencv.h:声明类与公共函数#pragma once#include <opencv
- 最近有由于需要,我开始接触阿里云的云市场的印刷文字识别-营业执照识别这里我加上了官网的申请说明,只要你有阿里云账号就可以用,前500次是免费
- 前言当大家使用mybatis作为持久层框架时,在存储和查询数据时,只需要在mapper.xml文件中配置好对应字段的JdbcType和Jav
- springboot整合tkmapper1.导入pom依赖1.1 导入springboot的parent依赖<parent> &
- 1、servlet层package com.ycz.controller;import com.alibaba.fastjson.JSON;
- Java 2D API通过扩展抽象窗口工具箱(AWT),为Java程序提供了二维图像,文本和图形的功能。这个复杂的渲染包支持线形图像,文本和
- 1、maven打包Spring Boot项目的pom.xml文件中默认使用spring-boot-maven-plugin插件进行打包:&l
- 前言本篇文章主要讲述的是SpringBoot整合Mybatis、Druid和PageHelper 并实现多数据源和分页。其中SpringBo
- 前言 同源策略:判断是否是同源的,主要看这三点,协议,ip,端口。同源策略就是浏览器出于网站安全性的考虑,限制不同源之间的资源相互访问的一种
- 需要注意的一点是,feign好像是无法传递list集合类型的,但是你可以通过传递对象类型,然后在接收方再次将对象装在集合中达到集合传递的效果
- MongoDB是介于关系数据库和非关系数据库之间的一种产品,文件的存储格式为BSON(一种JSON的扩展),这里就主要介绍Java通过使用m
- 一、使用Json.NetJson.Net是支持序列化和反序列化DataTable、DataSet、Entity Framework和Enti
- ApplicationContext简述ApplicationContext代表IOC容器,在SpringIOC容器中读取Bean配置创建B
- 报错信息如下:java: 无法访问org.springframework.boot.SpringApplication错误的类文件: /C:
- 1. 开方:Math.sqrt(x);2. x的a方:Math.pow(x,a);3. 绝对值:Math.abs(x);4. BigInte
- FastJson是阿里开源的一个高性能的JSON框架,FastJson数据处理速度快,无论序列化(把JavaBean对象转化成Json格式的
- 目录Directory:创建文件夹删除文件夹获取文件夹下的子文件夹获取同类型的文件判断文件夹是否存在移动文件夹总结之前发过File对文件的操