【Java】利用SpringBoot搭建WebService服务接口

2023-11-05

前言

在项目开发过程中经常会碰到对接医疗软件系统的时候对方要求提供WebService形式的接口,本篇文章记载了个人对接项目过程中整合并搭建的WebService形式的接口,希望对您能够有所帮助!


一、在pom.xml文件中导入WebService所需的Jar包

代码如下:

        <!--WebService-->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>stax2-api</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>woodstox-core-asl</artifactId>
            <version>4.4.1</version>
        </dependency>
        <!-- 这个主要是client访问的,但是问题多多-->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>
        <!--WebService-->

二、定义WebService接口实现类

1.RequestDTO通用入参实体类

代码如下(示例):

import lombok.Data;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

/**
 * @author 孤巷.
 * @description
 * @date 2022/8/5 11:47
 */
@XmlRootElement(name="ROOT")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"msgData", "loginToken","orgCode","type"})
@Data
public class RequestDTO {
    private static final long serialVersionUID = 3428504463675931746L;

    /**
     * 机构编码
     */
    String orgCode;

    /**
     * 业务方法
     */
    String type;

    /**
     * 业务参数,格式为JSON
     */
    String msgData;

    /**
     * 登录凭证
     */
    String loginToken;
}

2.impl接口实现类

代码如下(示例):

import com.example.web.dto.RequestDTO;

import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * @author 孤巷.
 * @description WebService函数定义
 * @date 2022/8/9 10:30
 */
@WebService(name = "qcsOdsImpl", targetNamespace = "http://server.webservice.example.com")
public interface qcsOdsImpl {

    /**
     * 门诊排队叫号通用函数
     * @param requestDTO 通用入参
     * @return String-SM4加密密文
     */
    String publicInterfaceFun(@WebParam(name="ROOT") RequestDTO requestDTO);

    /**
     * 智慧病房通用函数
     * @param requestDTO 通用入参
     * @return String-SM4加密密文
     */
    String smartWard(@WebParam(name="ROOT") RequestDTO requestDTO);

}

提示:其中的@WebParam(name="ROOT") 中的ROOT代表着方法的参数,与通用入参实体类RequestDTO中的一致即可

3.service业务类中引入实现类

代码如下(示例):

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.web.constant.Constant;
import com.example.web.dto.RequestDTO;
import com.example.web.model.MapMappingModel;
import com.example.web.service.InitDataService;
import com.example.web.util.MySM4Util;
import com.example.web.util.ReflectUtil;
import com.example.web.util.ResultUtil;
import com.example.web.util.SpringContextUtils;
import com.example.web.webservice.impl.qcsOdsImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.jws.WebService;
import java.lang.reflect.InvocationTargetException;

/**
 * @author 孤巷.
 * @description
 * @date 2022/8/9 10:30
 */
@Component
@WebService( targetNamespace = "http://server.webservice.example.com",
        endpointInterface = "com.example.web.webservice.impl.qcsOdsImpl")
@Slf4j
public class qcsOdsService implements qcsOdsImpl {

    /**
     * 门诊排队叫号通用函数
     * @param requestDTO 通用入参
     * @return String-SM4加密密文
     */
    @Override
    public String publicInterfaceFun(RequestDTO requestDTO) {
        return currencyService(requestDTO,1);
    }

    /**
     * 智慧病房通用函数
     * @param requestDTO 通用入参
     * @return String-SM4加密密文
     */
    @Override
    public String smartWard(RequestDTO requestDTO) {
        return currencyService(requestDTO,2);
    }



    /**
     * 通用业务
     * @param requestDTO 通用入参
     * @param type 1:智慧病房,2:门诊排队叫号
     * @return String
     */
    public String currencyService(RequestDTO requestDTO,int type){
        ........根据项目需求填写实际业务代码
        return null;
    }
    }
}

三、其他配置

1.application.yml

代码如下(示例):

cxf:
  path: /qcs_ods

2.启动类配置注解

代码如下(示例):

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
@EnableScheduling
@MapperScan(value = {"com.example.web.dao.*"})
@ComponentScan(basePackages = "com.example.web.*")
public class JavaWebserviceApplication {



    public static void main(String[] args) {
        SpringApplication.run(JavaWebserviceApplication.class, args);
    }

}

2.WebServiceConfig配置

代码如下(示例):

import javax.xml.ws.Endpoint;

import com.example.web.webservice.qcsOdsService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 孤巷.
 * @description
 * @date 2022/8/4 14:04
 */
@Configuration
public class WebServiceConfig {

    @Autowired
    private qcsOdsService serverServiceDemo;

    /**
     * Apache CXF 核心架构是以BUS为核心,整合其他组件。
     * Bus是CXF的主干, 为共享资源提供一个可配置的场所,作用类似于Spring的ApplicationContext,这些共享资源包括
     * WSDl管理器、绑定工厂等。通过对BUS进行扩展,可以方便地容纳自己的资源,或者替换现有的资源。默认Bus实现基于Spring架构,
     * 通过依赖注入,在运行时将组件串联起来。BusFactory负责Bus的创建。默认的BusFactory是SpringBusFactory,对应于默认
     * 的Bus实现。在构造过程中,SpringBusFactory会搜索META-INF/cxf(包含在 CXF 的jar中)下的所有bean配置文件。
     * 根据这些配置文件构建一个ApplicationContext。开发者也可以提供自己的配置文件来定制Bus。
     */
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    /**
     * 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问
     * 此方法被注释后, 即不改变前缀名(默认是services), wsdl访问地址为 http://127.0.0.1:8080/services/ws/api?wsdl
     * 去掉注释后wsdl访问地址为:http://127.0.0.1:8080/soap/ws/api?wsdl
     * http://127.0.0.1:8080/soap/列出服务列表 或 http://127.0.0.1:8080/soap/ws/api?wsdl 查看实际的服务
     * 新建Servlet记得需要在启动类添加注解:@ServletComponentScan
     *
     * 如果启动时出现错误:not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet
     * 可能是springboot与cfx版本不兼容。
     * 同时在spring boot2.0.6之后的版本与xcf集成,不需要在定义以下方法,直接在application.properties配置文件中添加:
     * cxf.path=/webservice(默认是services)
     */
//    @Bean
//    public ServletRegistrationBean dispatcherServlet() {
//        return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
//    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), serverServiceDemo);
        endpoint.publish("/services/qcsOdsService");
        return endpoint;
    }


}

四、访问地址

1.最后浏览器访问项目地址:http://localhost:9803/qcs_ods/services/qcsOdsService?wsdl
提示:项目访问地址的构成在代码中都有标注,请仔细核对并根据实际需求进行修改;

2.访问地址成功
在这里插入图片描述


总结

以上就是今天要讲的内容,本文仅仅简单介绍了WebService服务的使用,具体疑问可下方评论,感谢您的点赞以及评论;

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【Java】利用SpringBoot搭建WebService服务接口 的相关文章

随机推荐

  • Linux 查看当前路径下所有文件夹大小的方法

    进入需要查看的目录 例如 根目录 cd 查看当前目录下每个文件夹的大小 du sh 查看当前目录下每个文件夹的大小并排序 单位 字节 n 按照数值排序 du s sort n 补充 du sh 查看当前目录总共占的容量 而不单独列出各子项占
  • Task-Oriented Conversation Generation Using Heterogeneous Memory Networks

    EMNLP 2019 录用长文 Abstract 如何将外部知识库与对话模型结合起来是一个重要的问题 传统上人们是使用了Memory Network 然而当面对多种来源的 异构的info时 Mem对这些info的处理并不好 我理解的是权重的
  • Numpy基础数据结构

    Numpy基础数据结构 NumPy数组是一个多维数组对象 称为ndarray 其由两部分组成 实际的数据 描述这些数据的元数据 Numpy Python开源的科学计算工具包 高级的数据编程工具 ndarray 是强大的N维数组对象 对数据结
  • Eclipse导入项目No projects are found to import

    如果发现导入工程 impot 的时候 出现 No projects are found to import 的提示 首先查看项目目录中是否有隐藏文件 project 还有目录结构也还要有一个隐藏文件 classpath 如果没有 你可以参考
  • 不能不知道的OS模块的那些常用函数(附内置变量)

    文章目录 OS 模块 1 1 常用函数 1 2 使用示例 1 3 内置变量 1 4 附 内置变量详解 OS 模块 该模块提供了各种函数 允许您操作文件路径和检查与路径相关的信息 比如是否存在 文件扩展名 目录名等等 1 1 常用函数 其中一
  • [DIP]如何提取文件中的公章,并识别其朝向是否准确

    任务描述 我们需要知道 我们盖在文件上的红章是否是端正的 需要解决的问题 1 图章的识别 2 图章的定位 3 图章的方向判定 思路 图章基本上是红色的 我们先根据颜色提取可能的图章区域 当然 假如文档中 还有其他红色的区域 这一步都会提取出
  • 从Authy中导出账户和secret

    文章作者 GoodBoyboy 文章链接 https blog goodboyboy top posts 2689781648 html 版权声明 本博客所有文章除特别声明外 均采用 CC BY NC SA 4 0 许可协议 转载请注明来自
  • oracle bulk collect forall,Oracle批量绑定forallbulkcollect用法

    采用bulk collect 可以将查询结果一次性的加载到collections中 而不是通过CURSOR一条一条地处理 可以在select into fetchinto 采用bulk collect 可以将查询结果一次性的加载到colle
  • 银行项目测试

    主要的核心业务 存款业务 吸收客户的存款 为客户发放利息 属于负债业务 贷款业务 发放贷款给客户 收取客户的利息 属于银行的资产业务 中间业务 银行已中间人的身份 为客户办理业务 收取客户的手续费 例如 批量代发工资 批量代收水 电 燃气费
  • 华为OD机试 Python 【单词加密】

    题目 给你一句英文 里面有很多单词 单词间用空格隔开 我们要对这句子做点的变化 加密规则 如果单词里有元音 a e i o u 大小写都算 就把元音变成 如果一个单词完全没有元音 那就让这个单词的第一个和最后一个字母交换位置 输入 一句英文
  • SKB几个复制函数的区别

    1 skb clone Skb clone 函数只是复制sk buff结构 并不复制skb的数据缓冲区 Clone后的sk buff结构与原始的sk buff指向同一数据缓冲区 原始的和clone后的skb描述符的cloned值都会被置1
  • 前端基础之滚动显示

    marquee滚动标签 注 该标签已经过时 被w3c弃用 使用样例
  • 设计模式(一)- 模板方法模式

    模板方法模式 文章目录 模板方法模式 1 模板方法模式 1 介绍 2 应用实例 代码 1 父类抽象模板 统一方法定为final 2 子类去实现不同的方法 3 其他子类实现不同的方法 4 结果展示 1 模板方法模式 在模板模式 Templat
  • 微信小程序 功能页导航 functional-page-navigator 组件

    完整微信小程序 Java后端 技术贴目录清单页面 必看 仅在插件中有效 用于跳转到插件功能页 属性 类型 默认值 必填 说明 最低版本 version string release 否 跳转到的小程序版本 线上版本必须设置为 release
  • linux安装idea并创建快捷方式

    一 安装 1 下载 在linux自带的火狐浏览器打开下载更方便 下载 IntelliJ IDEA JetBrains 功能强大 符合人体工程学的 Java IDE 选择Linux 这里以下载社区版为例 我下载的版本是2022 2 3 文件名
  • 了解应用层

    应用层 1 概述 2 应用程序组织方式 2 1 C S方式 2 1 P2P方式 3 动态主机配置协议DHCP 3 1 DHCP工作流程 4 域名系统DNS 4 1 域名结构 4 2 域名分类 4 3 域名服务器 4 3 1 分类 4 4 D
  • Python-爬虫(Scrapy爬虫框架,爬取豆瓣读书和评分)

    文章目录 1 Scrapy注意点 2 Scrapy爬取豆瓣读书和评分 代码部分 数据定义items py 爬虫部分spiders book py 数据存储部分pipelines py 启动爬虫执行cmd命令 start py 1 Scrap
  • QT设计电子时钟类

    1 界面效果 2 类的声明 wedgit h class Widget public QWidget Q OBJECT public Widget QWidget parent nullptr 默认构造函数 Widget 默认析构函数 vo
  • 磁共振检查头部能检测出什么_头部核磁共振可以检查什么?

    很多人会疑惑为什么要做头部核磁共振 做CT不好吗 这是因为脑CT具有一定的局限性 有时候脑CT是不能诊断出脑部异常情况的 头部核磁共振检査较CT更为敏感 具有多方向切层 多参数成像的特点 能更精确的现实病变位置 范围大小及组织学特性 是发现
  • 【Java】利用SpringBoot搭建WebService服务接口

    前言 在项目开发过程中经常会碰到对接医疗软件系统的时候对方要求提供WebService形式的接口 本篇文章记载了个人对接项目过程中整合并搭建的WebService形式的接口 希望对您能够有所帮助 一 在pom xml文件中导入WebServ