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

这篇具有很好参考价值的文章主要介绍了【Java】利用SpringBoot搭建WebService服务接口。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

在项目开发过程中经常会碰到对接医疗软件系统的时候对方要求提供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.访问地址成功
springboot webservice接口开发,java,spring boot,web


总结

以上就是今天要讲的内容,本文仅仅简单介绍了WebService服务的使用,具体疑问可下方评论,感谢您的点赞以及评论;文章来源地址https://www.toymoban.com/news/detail-754685.html

到了这里,关于【Java】利用SpringBoot搭建WebService服务接口的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SpringBoot项目添加WebService服务

    1.简单描述 WebService简单理解就是用http发送接收xml数据,但这个xml得遵守系统的规范。这个规范就是WSDL(Web服务描述语言,Web Services Description Language)。 在WebService中传输的xml有一个正式的名称叫Soap(简单对象访问协议 Simple Object Access Protocol)。 WebService分为客户端和服务端

    2024年02月16日
    浏览(33)
  • 搭建大型分布式服务(四十六)利用mockito不启动SpringBoot项目下进行单元测试

    SpringBoot支持集成Mockito做单元测试,有时候SpringBoot有很多外部依赖,在本地很难启动或者启动时间很长,而我们只想对某个方法进行测试,需要怎样做呢? 一、本文要点 接前文,我们已经已介绍在两种mock模式下,怎样根据入参返回自定义mock结果,但都是通过启动SpringBoot项

    2024年02月06日
    浏览(35)
  • SpringBoot+MyBatisPlus+MySql+vue2+elementUi的案例、java访问数据库服务、java提供接口服务

    1、项目不使用前后端分离。 2、在创建 SpringBoot 的时候要注意各个插件间的版本问题。 3、后端技术 SpringBoot + MyBatisPlus + MySql 。 4、前端技术 vue2 + elementUi 。 简单介绍 1、数据库名称 ssm_db 2、表名称 tbl_book 数据表对象文件(Book.java) 配置文件(application.yml) 创建项目后,在 resou

    2024年02月10日
    浏览(41)
  • SpringBoot整合WebService

    WebService是一个比较旧的远程调用通信框架,现在企业项目中用的比较少,因为它逐步被SpringCloud所取代,它的优势就是能够跨语言平台通信,所以还有点价值,下面来看看如何在SpringBoot项目中使用WebService 我们模拟从WebService客户端发送请求给WebService服务端暴露的下载文件服

    2024年02月15日
    浏览(24)
  • SpringBoot 整合WebService详解

    WebService服务端是以远程接口为主的,在Java实现的WebService技术里主要依靠CXF开发框架,而这个CXF开发框架可以直接将接口发布成WebService。 CXF又分为JAX-WS和JAX-RS,JAX-WS是基于xml协议,而JAX-RS是基于Restful风格,两者的区别如下: RS基于Restful风格,WS基于SOAP的XML协议 RS比WS传输的

    2024年02月04日
    浏览(26)
  • springboot调用webservice简便方式

    此方法不建议在需要 高并发 或者是 批量调用 webservice接口时使用,比较吃内存。仅在管理系统后台中,或者是用户量少时可以采用此取巧方案。 直接上核心代码:

    2024年02月14日
    浏览(20)
  • [webservice] springboot整合cxf

    2.1 pom.xml 中添加依赖 springboot整合Apache cxf 2.3 service发布(cxf配置) 发布完成后启动web服务,可以通过: http://127.0.0.1:8080/ws 访问已经发布的服务。 在这里分享一个天气预报的webservice服务: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl 3.1 使用wsdl2java生成webservice客户端代码

    2024年04月25日
    浏览(21)
  • WebService工具类(SpringBoot环境调用)

    以下工具集成了多种方式调用webservice,如http方式,axis方式,动态生成客户端方式等 ,是为笔者实际工作中提炼的,方便大家直接套用,常用方法都有调用示列。 一、整个工具类代码 二、pom依赖引入 可能有复制多了的依赖,可自己尝试去除,工具类不飘红报错就行 三、方

    2024年02月04日
    浏览(25)
  • 后端Springboot框架搭建APi接口开发(第一章)

    本文章以IDEA为开发工具,使用SSM框架进行项目编写 我们用一个简单的用户表进行操作演示 首先创建 Data 数据库 创建 User 数据表,表中包含 用户邮箱 , 用户姓名 , 用户密码 放点数据进去 打开IDEA,在右上角点击NewProject。创建新的项目 选择Spring Initializr框架,依次输入项目

    2023年04月08日
    浏览(31)
  • 加密无忧:SpringBoot中快速搭建安全的API接口

    在项目中,为了保证数据的安全,我们常常会对传递的数据进行加密。常用的加密算法包括对称加密(AES)和非对称加密(RSA),博主选取码云上最简单的API加密项目进行下面的讲解。 https://gitee.com/isuperag/rsa-encrypt-body-spring-boot 该项目使用RSA加密方式对API接口返回的数据加密

    2024年04月22日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包