欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot使用webservice發(fā)布和調(diào)用接口的實(shí)例詳解

 更新時間:2024年10月12日 09:42:09   作者:z不像程序員的程序猿  
本文介紹了如何在Springboot中使用webservice發(fā)布和調(diào)用接口,涵蓋了必要的依賴添加和代碼示例,文中提供了服務(wù)端和客戶端的實(shí)現(xiàn)方法,以及如何設(shè)置端口和服務(wù)地址,幫助讀者更好地理解和應(yīng)用Springboot結(jié)合webservice的技術(shù)

springboot使用webservice發(fā)布和調(diào)用接口

加入以下依賴:

<!-- cxf框架依賴  -->
	<dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
		<version>3.2.4</version>
	</dependency>

服務(wù)端service代碼:

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
 * TODO(描述這個類的作用) 
 * @author zyl
 * @date 2019年3月27日
 */
/**
 * 創(chuàng)建服務(wù)接口
 * @author oKong
 *
 */
@WebService()
public interface HelloWebService {
    @WebMethod
    public String Hello(@WebParam(name="name") String name);
}

服務(wù)端實(shí)現(xiàn)類代碼:

import javax.jws.WebParam;
import javax.jws.WebService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * TODO(描述這個類的作用) 
 * @author zyl
 * @date 2019年3月27日
 */
@WebService(
        targetNamespace = "demo.example.com", //wsdl命名空間 
        serviceName = "HelloWebService",                 //portType名稱 客戶端生成代碼時 為接口名稱
        endpointInterface = "com.example.demo.configuraction.webservice.HelloWebService")//指定發(fā)布webservcie的接口類,此類也需要接入@WebService注解
@Configuration
public class HelloWebServiceImpl implements HelloWebService{
    @Override
    public String Hello(@WebParam(name="name") String name) {
        System.out.println("歡迎你"+name);
        return "歡迎你"+name;
    }
}

服務(wù)端發(fā)布服務(wù)類:
我的端口設(shè)置為9999,所以我的服務(wù)地址為http://127.0.0.1:9090/ws/helloWebService?wsdl

import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * cxf配置類
 * @author oKong
 *
 */
@Configuration
public class CxfWebServiceConfig {
    @Autowired
    private Bus bus;
    @Autowired
    private HelloWebService helloWebService;
    @Bean("cxfServletRegistration")
    public ServletRegistrationBean dispatcherServlet() {
        //注冊servlet 攔截/ws 開頭的請求 不設(shè)置 默認(rèn)為:/services/*
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
    }
    /*
     * 發(fā)布endpoint
     */
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, helloWebService);
        endpoint.publish("/helloWebService");//發(fā)布地址
        return endpoint;
    }
}

客戶端調(diào)用服務(wù)代碼:
以下兩種方法選一即可

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
/**
 * @ClassName:CxfClient
 * @Description:webservice客戶端:
 *                 該類提供兩種不同的方式來調(diào)用webservice服務(wù)
 *              1:代理工廠方式
 *              2:動態(tài)調(diào)用webservice
 * @author Jerry
 * @date:2018年4月10日下午4:14:07
 */
public class CxfClient {
    public static void main(String[] args) {
        CxfClient.main1();
//        CxfClient.main2();
    }
    /**
     * 1.代理類工廠的方式,需要拿到對方的接口地址
     */
    public static void main1() {
        try {
            // 接口地址
            String address = "http://127.0.0.1:9090/ws/helloWebService?wsdl";
            // 代理工廠
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 設(shè)置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            // 設(shè)置接口類型
            jaxWsProxyFactoryBean.setServiceClass(HelloWebService.class);
            // 創(chuàng)建一個代理接口實(shí)現(xiàn)
            HelloWebService us = (HelloWebService) jaxWsProxyFactoryBean.create();
            // 數(shù)據(jù)準(zhǔn)備
            String userId = "zz";
            // 調(diào)用代理接口的方法調(diào)用并返回結(jié)果
            String result = us.Hello(userId);
            System.out.println("返回結(jié)果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 2:動態(tài)調(diào)用
     */
    public static void main2() {
        // 創(chuàng)建動態(tài)客戶端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://127.0.0.1:9090/ws/helloWebService?wsdl");
        // 需要密碼的情況需要加上用戶名和密碼
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",參數(shù)1,參數(shù)2,參數(shù)3....);
            objects = client.invoke("getUserName", "maple");
            System.out.println("返回數(shù)據(jù):" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
}

到此這篇關(guān)于springboot使用webservice發(fā)布和調(diào)用接口的文章就介紹到這了,更多相關(guān)springboot webservice發(fā)布和調(diào)用接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JDK8?中Arrays.sort()?排序方法詳解

    JDK8?中Arrays.sort()?排序方法詳解

    這篇文章主要介紹了JDK8?中Arrays.sort()?排序方法解讀,本文先行介紹Arrays.sort()中影響排序方式的幾個因素,影響因素主要為數(shù)組類型、數(shù)組大小,結(jié)合閾值對排序方式進(jìn)行選擇,需要的朋友可以參考下
    2023-05-05
  • Java中數(shù)字黑洞實(shí)現(xiàn)代碼

    Java中數(shù)字黑洞實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java編程中如何實(shí)現(xiàn)數(shù)字黑洞算法游戲,其中涉及到了數(shù)組、scanner、if語句等Java編程的基礎(chǔ)知識,需要的朋友可以參考下
    2017-09-09
  • SpringBoot 整合 Shiro 密碼登錄的實(shí)現(xiàn)代碼

    SpringBoot 整合 Shiro 密碼登錄的實(shí)現(xiàn)代碼

    這篇文章主要介紹了SpringBoot 整合 Shiro 密碼登錄的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • Java基礎(chǔ)之容器Vector詳解

    Java基礎(chǔ)之容器Vector詳解

    這篇文章主要介紹了Java基礎(chǔ)之容器Vector詳解,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-04-04
  • 基于SpringBoot的SSMP的整合案例

    基于SpringBoot的SSMP的整合案例

    這篇文章主要介紹了SpringBoot整合SSMP的詳細(xì)教程,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • lombok的@EqualsAndHashcode注解詳解

    lombok的@EqualsAndHashcode注解詳解

    這篇文章主要介紹了lombok的@EqualsAndHashcode注解的用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • JAVA實(shí)現(xiàn)遍歷文件夾下的所有文件(遞歸調(diào)用和非遞歸調(diào)用)

    JAVA實(shí)現(xiàn)遍歷文件夾下的所有文件(遞歸調(diào)用和非遞歸調(diào)用)

    本篇文章主要介紹了JAVA 遍歷文件夾下的所有文件(遞歸調(diào)用和非遞歸調(diào)用) ,具有一定的參考價值,有興趣的可以了解一下。
    2017-01-01
  • 基于jstree使用JSON數(shù)據(jù)組裝成樹

    基于jstree使用JSON數(shù)據(jù)組裝成樹

    這篇文章主要為大家詳細(xì)介紹了基于jstree使用JSON數(shù)據(jù)組裝成樹,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • SpringBoot Bean被加載時進(jìn)行控制

    SpringBoot Bean被加載時進(jìn)行控制

    很多時候我們需要根據(jù)不同的條件在容器中加載不同的Bean,或者根據(jù)不同的條件來選擇是否在容器中加載某個Bean,這就是Bean的加載控制,一般我們可以通過編程式或注解式兩種不同的方式來完成Bean的加載控制
    2023-02-02
  • 深入學(xué)習(xí)spring cloud gateway 限流熔斷

    深入學(xué)習(xí)spring cloud gateway 限流熔斷

    這篇文章主要介紹了深入學(xué)習(xí)spring cloud gateway 限流熔斷,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04

最新評論