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-hk/n/297967.html