使用SpringBoot Webservice進行接口開發與調用

SpringBoot Webservice 是 Spring Framework 的重要組成部分,提供了輕量級的模塊化解決方案,並且能夠快速構建各種 RESTful 模式的 Web Services 以便與其他系統進行通信。

本文將從如下幾個方面對 SpringBoot Webservice 進行詳細的闡述:

一、SpringBoot Webservice 簡介

Spring Framework 是主要用於企業級 Java 應用程序的一個開源框架。SpringBoot Webservice 是其重要組成部分,通過提供模塊化的輕量級解決方案,能夠快速構建各種形式的 Web 服務。在 SpringBoot 中使用 Web Service 的最大優勢之一是其模塊化,這意味着你可以只使用你需要的部分,而且這些部分在運行時被自動裝配。SpringBoot Webservice 還能夠很好地與 JAX-WS 和 JAXB 編組等標準結合使用,滿足不同的應用需求。

二、SpringBoot Webservice 接口調用

1、使用 CXF 客戶端進行調用

@Bean
public JaxWsProxyFactoryBean jaxWsProxyFactoryBean() {
    JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
    factoryBean.setServiceClass(Calculator.class);
    factoryBean.setAddress("http://localhost:8080/calculator");
    return factoryBean;
}

@Autowired
private JaxWsProxyFactoryBean jaxWsProxyFactoryBean;

@Test
public void testAdd() {
    Calculator calculator = jaxWsProxyFactoryBean.create(Calculator.class);
    assertEquals(calculator.add(10, 20), 30);
}

2、使用 SoapUI 進行調用

SoapUI 是一種非常強大的 Soap API 測試工具。它可以用於測試 Web 服務、RESTful API 以及其他類型的 Web API。使用 SoapUI 進行測試可以模擬發送請求的過程,使你可以快速識別潛在的問題。

3、使用 Java 應用程序進行調用

使用 Java 應用程序調用 SpringBoot Webservice 接口的最簡單方法是使用 SAAJ API。簡單說來,SAAJ 是一個在 Java 中構建 SOAP 消息的標準 API。以下是一個簡單的示例:

@WebService(endpointInterface = "com.example.demo.service.Calculator")
public class CalculatorImpl implements Calculator {
    @Override
    public int add(int a, int b) {
        return a + b;
    }
}

SoapConnectionFactory soapConnectionFactory = SoapConnectionFactory.newInstance();
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPConnection connection = soapConnectionFactory.createConnection();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPHeader soapHeader = soapEnvelope.getHeader();
SOAPBody soapBody = soapEnvelope.getBody();
SOAPElement soapElement = soapBody.addChildElement("add", "ns1", "http://www.example.org/service");
SOAPElement soapChildElement1 = soapElement.addChildElement("arg0");
SOAPElement soapChildElement2 = soapElement.addChildElement("arg1");
soapChildElement1.addTextNode("10");
soapChildElement2.addTextNode("20");
URL endpoint = new URL("http://localhost:8080/calculator");
SOAPMessage response = connection.call(soapMessage, endpoint);
SOAPBody responseBody = response.getSOAPBody();
SOAPElement responseElement = (SOAPElement) responseBody.getChildElements().next();
String result = responseElement.getValue();
assertEquals(Integer.parseInt(result), 30);

三、SpringBoot Webservice 接口開發

1、基本註解

在 SpringBoot Webservice 中,有很多註解可以用來處理實現 Web Service 的 Java 類。其中最常用的是 @WebService,用於將一個 Java 類標記為 Web Service。以下是一個簡單的 @WebService 標註的例子:

@WebService(endpointInterface = "com.example.demo.service.Calculator")
public class CalculatorImpl implements Calculator {
    @Override
    public int add(int a, int b) {
        return a + b;
    }
}

2、@WebService 註解屬性

@WebService 提供了很多屬性,用於調整 Web Service 的行為。以下是常見的一些:

  • name:指定 Web Service 的名稱。
  • portName:指定端口的名稱。
  • serviceName:指定 Service 的名稱。
  • targetNamespace:指定 XML 中的命名空間。

3、如何發布 Web 服務

通過使用 SpringBoot 功能,可以在 Web 容器中輕鬆發布 Web 服務。定義一個新的 SpringBoot 應用程序,並且在其 main 類中添加以下內容:

@Endpoint
public class Calculator {
    @PayloadRoot(localPart = "add", namespace = "http://www.example.org/service")
    @ResponsePayload
    public AddResponse add(@RequestPayload Add request) {
        AddResponse response = new AddResponse();
        response.setResult(request.getArg0() + request.getArg1());
        return response;
    }
}

@Bean
public ServletRegistrationBean messageDispatcherServlet() {
    MessageDispatcherServlet servlet = new MessageDispatcherServlet();
    servlet.setTransformWsdlLocations(true);
    return new ServletRegistrationBean(servlet, "/demo/*");
}

@Bean(name = "demo")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema schema) {
    DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
    wsdl11Definition.setPortTypeName("Calculator");
    wsdl11Definition.setLocationUri("/demo");
    wsdl11Definition.setTargetNamespace("http://www.example.org/service");
    wsdl11Definition.setSchema(schema);
    return wsdl11Definition;
}

@Bean
public XsdSchema schema() {
    return new SimpleXsdSchema(new ClassPathResource("demo.xsd"));
}

4、在 Web 服務中使用 Spring Security

Spring Security 是一個可定製的安全框架,用於保護 Spring 應用程序中的 Web 資源。下面是如何使用 Spring Security 在 Web 服務中進行身份驗證的一個簡單示例:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .httpBasic();
    }
}

四、SpringBoot Webservice 示例代碼

完整代碼請訪問:https://github.com/coolspan/springboot-webservice-example

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/297967.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-28 12:17
下一篇 2024-12-28 12:17

相關推薦

  • 從ga角度解讀springboot

    springboot作為目前廣受歡迎的Java開發框架,其中的ga機制在整個開發過程中起着至關重要的作用。 一、ga是什麼 ga即Group Artifacts的縮寫,它是Mave…

    編程 2025-04-29
  • Java 監控接口返回信息報錯信息怎麼處理

    本文將從多個方面對 Java 監控接口返回信息報錯信息的處理方法進行詳細的闡述,其中包括如何捕獲異常、如何使用日誌輸出錯誤信息、以及如何通過異常處理機制解決報錯問題等等。以下是詳細…

    編程 2025-04-29
  • Python接口自動化測試

    本文將從如下多個方面對Python編寫接口自動化進行詳細闡述,包括基本介紹、常用工具、測試框架、常見問題及解決方法 一、基本介紹 接口自動化測試是軟件測試中的一種自動化測試方式。通…

    編程 2025-04-27
  • SpringBoot Get方式請求傳參用法介紹

    本文將從以下多個方面對SpringBoot Get方式請求傳參做詳細的闡述,包括URL傳參、路徑傳參、請求頭傳參、請求體傳參等,幫助讀者更加深入地了解Get請求方式下傳參的相關知識…

    編程 2025-04-27
  • Jadoor門鎖開發接口接入指南

    本文將從多個方面詳細介紹如何將門鎖接入Jadoor平台的開發接口,方便開發者們快速實現門鎖遠程控制、開鎖記錄查看等功能。 一、Jadoor門鎖開發接口簡介 Jadoor是一款用於密…

    編程 2025-04-27
  • 後端接口設計開發經驗分享

    在受到前端某些限制或特殊需求時,後端接口的設計和開發顯得尤為重要。下面從以下幾個方面進行講述。 一、命名規範 合理的命名規範可以大大提高接口的可讀性和可維護性。以下是一些命名規範的…

    編程 2025-04-27
  • SpringBoot如何設置不輸出Info日誌

    本篇文章將帶您了解如何在SpringBoot項目中關閉Info級別日誌輸出。 一、為什麼要關閉Info日誌 在開發中,我們經常會使用Log4j、Logback等框架來輸出日誌信息,…

    編程 2025-04-27
  • 期貨數據接口 Python:打通數字資產交易數據的關鍵

    本文將從以下幾個方面討論期貨數據接口 Python: 一、數據接口簡介 期貨數據接口是指為期貨從業人員提供用於獲取歷史、實時及未來交易數據的工具。Python是一種常用的編程語言,…

    編程 2025-04-27
  • 如何快速發布http接口

    想要快速發布http接口,可以從以下幾個方面入手。 一、選擇合適的框架 選擇合適的框架對於快速發布http接口非常重要。目前比較受歡迎的框架有Flask、Django、Tornad…

    編程 2025-04-27
  • Javaweb 接口返回數據的定義與實現

    本文將介紹 javaweb 如何定義接口返回數據,並提供相應的代碼示例。 一、接口返回數據的定義 在 javaweb 開發中,我們經常需要通過接口返回數據。接口返回的數據格式通常是…

    編程 2025-04-27

發表回復

登錄後才能評論