最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何利用SpringBoot搭建WebService服務(wù)接口

 更新時(shí)間:2023年11月23日 09:13:37   作者:孤巷守鶴  
之前項(xiàng)目經(jīng)理想要開發(fā)一個(gè)webservice的協(xié)議,給我一個(gè)星期的時(shí)間,后面用springboot開發(fā)了webservice,這篇文章主要給大家介紹了關(guān)于如何利用SpringBoot搭建WebService服務(wù)接口的相關(guān)資料,需要的朋友可以參考下

前言

在項(xiàng)目開發(fā)過程中經(jīng)常會(huì)碰到對(duì)接醫(yī)療軟件系統(tǒng)的時(shí)候?qū)Ψ揭筇峁¦ebService形式的接口,本篇文章記載了個(gè)人對(duì)接項(xiàng)目過程中整合并搭建的WebService形式的接口,希望對(duì)您能夠有所幫助!

一、在pom.xml文件中導(dǎo)入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>
        <!-- 這個(gè)主要是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接口實(shí)現(xiàn)類

1.RequestDTO通用入?yún)?shí)體類

代碼如下(示例):

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;

    /**
     * 機(jī)構(gòu)編碼
     */
    String orgCode;

    /**
     * 業(yè)務(wù)方法
     */
    String type;

    /**
     * 業(yè)務(wù)參數(shù),格式為JSON
     */
    String msgData;

    /**
     * 登錄憑證
     */
    String loginToken;
}

2.impl接口實(shí)現(xiàn)類

代碼如下(示例):

import com.example.web.dto.RequestDTO;

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

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

    /**
     * 門診排隊(duì)叫號(hào)通用函數(shù)
     * @param requestDTO 通用入?yún)?
     * @return String-SM4加密密文
     */
    String publicInterfaceFun(@WebParam(name="ROOT") RequestDTO requestDTO);

    /**
     * 智慧病房通用函數(shù)
     * @param requestDTO 通用入?yún)?
     * @return String-SM4加密密文
     */
    String smartWard(@WebParam(name="ROOT") RequestDTO requestDTO);

}

提示:其中的@WebParam(name="ROOT") 中的ROOT代表著方法的參數(shù),與通用入?yún)?shí)體類RequestDTO中的一致即可

3.service業(yè)務(wù)類中引入實(shí)現(xiàn)類

代碼如下(示例):

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 {

    /**
     * 門診排隊(duì)叫號(hào)通用函數(shù)
     * @param requestDTO 通用入?yún)?
     * @return String-SM4加密密文
     */
    @Override
    public String publicInterfaceFun(RequestDTO requestDTO) {
        return currencyService(requestDTO,1);
    }

    /**
     * 智慧病房通用函數(shù)
     * @param requestDTO 通用入?yún)?
     * @return String-SM4加密密文
     */
    @Override
    public String smartWard(RequestDTO requestDTO) {
        return currencyService(requestDTO,2);
    }

    /**
     * 通用業(yè)務(wù)
     * @param requestDTO 通用入?yún)?
     * @param type 1:智慧病房,2:門診排隊(duì)叫號(hào)
     * @return String
     */
    public String currencyService(RequestDTO requestDTO,int type){
        ........根據(jù)項(xiàng)目需求填寫實(shí)際業(yè)務(wù)代碼
        return null;
    }
    }
}

三、其他配置

1.application.yml

代碼如下(示例):

cxf:
  path: /qcs_ods

2.啟動(dòng)類配置注解

代碼如下(示例):

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 核心架構(gòu)是以BUS為核心,整合其他組件。
     * Bus是CXF的主干, 為共享資源提供一個(gè)可配置的場所,作用類似于Spring的ApplicationContext,這些共享資源包括
     * WSDl管理器、綁定工廠等。通過對(duì)BUS進(jìn)行擴(kuò)展,可以方便地容納自己的資源,或者替換現(xiàn)有的資源。默認(rèn)Bus實(shí)現(xiàn)基于Spring架構(gòu),
     * 通過依賴注入,在運(yùn)行時(shí)將組件串聯(lián)起來。BusFactory負(fù)責(zé)Bus的創(chuàng)建。默認(rèn)的BusFactory是SpringBusFactory,對(duì)應(yīng)于默認(rèn)
     * 的Bus實(shí)現(xiàn)。在構(gòu)造過程中,SpringBusFactory會(huì)搜索META-INF/cxf(包含在 CXF 的jar中)下的所有bean配置文件。
     * 根據(jù)這些配置文件構(gòu)建一個(gè)ApplicationContext。開發(fā)者也可以提供自己的配置文件來定制Bus。
     */
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    /**
     * 此方法作用是改變項(xiàng)目中服務(wù)名的前綴名,此處127.0.0.1或者localhost不能訪問時(shí),請(qǐng)使用ipconfig查看本機(jī)ip來訪問
     * 此方法被注釋后, 即不改變前綴名(默認(rèn)是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/列出服務(wù)列表 或 http://127.0.0.1:8080/soap/ws/api?wsdl 查看實(shí)際的服務(wù)
     * 新建Servlet記得需要在啟動(dòng)類添加注解:@ServletComponentScan
     *
     * 如果啟動(dòng)時(shí)出現(xiàn)錯(cuò)誤:not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet
     * 可能是springboot與cfx版本不兼容。
     * 同時(shí)在spring boot2.0.6之后的版本與xcf集成,不需要在定義以下方法,直接在application.properties配置文件中添加:
     * cxf.path=/webservice(默認(rèn)是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.最后瀏覽器訪問項(xiàng)目地址:http://localhost:9803/qcs_ods/services/qcsOdsService?wsdl提示:項(xiàng)目訪問地址的構(gòu)成在代碼中都有標(biāo)注,請(qǐng)仔細(xì)核對(duì)并根據(jù)實(shí)際需求進(jìn)行修改;

2.訪問地址成功

總結(jié)

到此這篇關(guān)于如何利用SpringBoot搭建WebService服務(wù)接口的文章就介紹到這了,更多相關(guān)SpringBoot搭建WebService服務(wù)接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java詳細(xì)分析LCN框架分布式事務(wù)

    Java詳細(xì)分析LCN框架分布式事務(wù)

    這篇文章主要介紹了Java LCN框架分布式事務(wù),分布式事務(wù)是指事務(wù)的參與者、支持事務(wù)的服務(wù)器、資源服務(wù)器以及事務(wù)管理器分別位于不同的分布式系統(tǒng)的不同節(jié)點(diǎn)之上
    2022-07-07
  • 關(guān)于maven本地倉庫的配置方式

    關(guān)于maven本地倉庫的配置方式

    這篇文章主要介紹了關(guān)于maven本地倉庫的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 編寫Java代碼對(duì)HDFS進(jìn)行增刪改查操作代碼實(shí)例

    編寫Java代碼對(duì)HDFS進(jìn)行增刪改查操作代碼實(shí)例

    這篇文章主要介紹了Java代碼對(duì)HDFS進(jìn)行增刪改查操作,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 詳解使用Spring Security進(jìn)行自動(dòng)登錄驗(yàn)證

    詳解使用Spring Security進(jìn)行自動(dòng)登錄驗(yàn)證

    本篇文章主要介紹了詳解使用Spring Security進(jìn)行自動(dòng)登錄驗(yàn)證,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-09-09
  • Java無界阻塞隊(duì)列DelayQueue詳細(xì)解析

    Java無界阻塞隊(duì)列DelayQueue詳細(xì)解析

    這篇文章主要介紹了Java無界阻塞隊(duì)列DelayQueue詳細(xì)解析,DelayQueue是一個(gè)支持時(shí)延獲取元素的無界阻塞隊(duì)列,隊(duì)列使用PriorityQueue來實(shí)現(xiàn),隊(duì)列中的元素必須實(shí)現(xiàn)Delayed接口,在創(chuàng)建元素時(shí)可以指定多久才能從隊(duì)列中獲取當(dāng)前元素,需要的朋友可以參考下
    2023-12-12
  • Mybatis的類型轉(zhuǎn)換接口TypeHandler

    Mybatis的類型轉(zhuǎn)換接口TypeHandler

    這篇文章主要介紹了Mybatis的類型轉(zhuǎn)換接口TypeHandler,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-08-08
  • 導(dǎo)入SpringCloud依賴踩的坑及解決

    導(dǎo)入SpringCloud依賴踩的坑及解決

    這篇文章主要介紹了導(dǎo)入SpringCloud依賴踩的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行sql腳本的方法步驟

    SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行sql腳本的方法步驟

    本文主要介紹了SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行sql腳本的方法步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • springboot?vue測(cè)試平臺(tái)接口定義及發(fā)送請(qǐng)求功能實(shí)現(xiàn)

    springboot?vue測(cè)試平臺(tái)接口定義及發(fā)送請(qǐng)求功能實(shí)現(xiàn)

    這篇文章主要為大家介紹了springboot+vue測(cè)試平臺(tái)接口定義及發(fā)送請(qǐng)求功能實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Springmvc獲取前臺(tái)請(qǐng)求數(shù)據(jù)過程解析

    Springmvc獲取前臺(tái)請(qǐng)求數(shù)據(jù)過程解析

    這篇文章主要介紹了Springmvc獲取前臺(tái)請(qǐng)求數(shù)據(jù)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07

最新評(píng)論

诏安县| 揭阳市| 永善县| 石台县| 新邵县| 余干县| 崇明县| 赫章县| 绥芬河市| 隆回县| 织金县| 阿尔山市| 肇东市| 绿春县| 梨树县| 富阳市| 湛江市| 阿坝县| 奈曼旗| 丰县| 宜州市| 卓尼县| 高碑店市| 永春县| 资兴市| 来宾市| 保定市| 彰化市| 房山区| 类乌齐县| 平安县| 岐山县| 祁阳县| 平陆县| 玛曲县| 北碚区| 比如县| 甘孜县| 黔东| 桂平市| 宾阳县|