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

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

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

前言

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

一、在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>
        <!-- 這個主要是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接口實現(xiàn)類

1.RequestDTO通用入?yún)嶓w類

代碼如下(示例):

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;

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

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

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

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

2.impl接口實現(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 {

    /**
     * 門診排隊叫號通用函數(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)嶓w類RequestDTO中的一致即可

3.service業(yè)務(wù)類中引入實現(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 {

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

    /**
     * 此方法作用是改變項目中服務(wù)名的前綴名,此處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/列出服務(wù)列表 或 http://127.0.0.1:8080/soap/ws/api?wsdl 查看實際的服務(wù)
     * 新建Servlet記得需要在啟動類添加注解:@ServletComponentScan
     *
     * 如果啟動時出現(xiàn)錯誤: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提示:項目訪問地址的構(gòu)成在代碼中都有標注,請仔細核對并根據(jù)實際需求進行修改;

2.訪問地址成功

總結(jié)

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

相關(guān)文章

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

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

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

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

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

    編寫Java代碼對HDFS進行增刪改查操作代碼實例

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

    詳解使用Spring Security進行自動登錄驗證

    本篇文章主要介紹了詳解使用Spring Security進行自動登錄驗證,非常具有實用價值,需要的朋友可以參考下
    2017-09-09
  • Java無界阻塞隊列DelayQueue詳細解析

    Java無界阻塞隊列DelayQueue詳細解析

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

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

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

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

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

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

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

    springboot?vue測試平臺接口定義及發(fā)送請求功能實現(xiàn)

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

    Springmvc獲取前臺請求數(shù)據(jù)過程解析

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

最新評論

宕昌县| 额敏县| 潮安县| 东阿县| 华亭县| 四子王旗| 商都县| 陆河县| 扎赉特旗| 克什克腾旗| 普兰店市| 克山县| 鄂托克前旗| 上虞市| 游戏| 张家口市| 嫩江县| 星子县| 屏边| 女性| 南川市| 双辽市| 沙河市| 麻阳| 扎鲁特旗| 曲阳县| 两当县| 浙江省| 肇源县| 邯郸县| 清涧县| 泾川县| 宁安市| 汝阳县| 上虞市| 额敏县| 肃宁县| 通州区| 巴彦县| 富蕴县| 青河县|