一、SOAP和RESTful協議
SOAP(Simple Object Access Protocol)和RESTful(Representational State Transfer)是兩種常見的Web Service協議。SOAP是基於XML和HTTP協議的,是一種重量級協議;RESTful則是基於HTTP協議的,主要使用JSON數據格式,是一種輕量級協議。
在Spring Boot中,可以選擇使用SOAP或RESTful協議進行Web Service介面調用。如果需要保證數據的安全性和事務一致性,SOAP是一個不錯的選擇;如果介面簡單,數據格式較為簡單,則可以選擇RESTful協議。
二、SOAP協議下的Web Service介面調用
1、首先,在pom.xml中加入以下依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency>
2、在Spring Boot主類中添加@EnableWs和@Bean註解,開啟Web Service支持,並定義WebService介面:
@SpringBootApplication @EnableWs public class DemoApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DemoApplication.class); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Bean public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) { MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(applicationContext); servlet.setTransformWsdlLocations(true); return new ServletRegistrationBean(servlet, "/ws/*"); } @Bean(name = "countries") public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) { DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition(); wsdl11Definition.setPortTypeName("CountriesPort"); wsdl11Definition.setLocationUri("/ws"); wsdl11Definition.setTargetNamespace("http://spring.io/guides/gs-producing-web-service"); wsdl11Definition.setSchema(countriesSchema); return wsdl11Definition; } @Bean public XsdSchema countriesSchema() { return new SimpleXsdSchema(new ClassPathResource("countries.xsd")); } @WebService public interface CountryService { @WebMethod String getCapital(String countryName); @WebMethod String getCurrency(String countryName); } @Service public class CountryServiceImpl implements CountryService { private Map<String, Country> countries = new HashMap<>(); public CountryServiceImpl() { Country spain = new Country(); spain.setName("Spain"); spain.setCapital("Madrid"); spain.setCurrency(Currency.EUR); countries.put(spain.getName(), spain); Country poland = new Country(); poland.setName("Poland"); poland.setCapital("Warsaw"); poland.setCurrency(Currency.PLN); countries.put(poland.getName(), poland); Country uk = new Country(); uk.setName("United Kingdom"); uk.setCapital("London"); uk.setCurrency(Currency.GBP); countries.put(uk.getName(), uk); } @Override public String getCapital(String countryName) { return countries.get(countryName).getCapital(); } @Override public String getCurrency(String countryName) { return countries.get(countryName).getCurrency().toString(); } } public enum Currency { EUR, GBP, PLN } public class Country { private String name; private String capital; private Currency currency; public String getName() { return name; } public void setName(String value) { this.name = value; } public String getCapital() { return capital; } public void setCapital(String value) { this.capital = value; } public Currency getCurrency() { return currency; } public void setCurrency(Currency value) { this.currency = value; } } }
3、在resources目錄下創建一個countries.xsd文件,定義數據格式:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://spring.io/guides/gs-producing-web-service" targetNamespace="http://spring.io/guides/gs-producing-web-service" elementFormDefault="qualified"> <xs:element name="getCapitalRequest"> <xs:complexType> <xs:sequence> <xs:element name="arg0" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="getCapitalResponse"> <xs:complexType> <xs:sequence> <xs:element name="return" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="getCurrencyRequest"> <xs:complexType> <xs:sequence> <xs:element name="arg0" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="getCurrencyResponse"> <xs:complexType> <xs:sequence> <xs:element name="return" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
4、啟動項目,並使用SOAPUI等工具進行介面測試。
三、RESTful協議下的Web Service介面調用
1、首先,在pom.xml中加入以下依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.11.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> <version>2.11.1</version> </dependency>
2、定義一個Controller類,實現介面方法:
@RestController public class CountryController { private Map<String, Country> countries = new HashMap<>(); public CountryController() { Country spain = new Country(); spain.setName("Spain"); spain.setCapital("Madrid"); spain.setCurrency(Currency.EUR); countries.put(spain.getName(), spain); Country poland = new Country(); poland.setName("Poland"); poland.setCapital("Warsaw"); poland.setCurrency(Currency.PLN); countries.put(poland.getName(), poland); Country uk = new Country(); uk.setName("United Kingdom"); uk.setCapital("London"); uk.setCurrency(Currency.GBP); countries.put(uk.getName(), uk); } @RequestMapping(value = "/capital/{name}", method = RequestMethod.GET) public String getCapital(@PathVariable("name") String name) { return countries.get(name).getCapital(); } @RequestMapping(value = "/currency/{name}", method = RequestMethod.GET) public String getCurrency(@PathVariable("name") String name) { return countries.get(name).getCurrency().toString(); } public enum Currency { EUR, GBP, PLN } public class Country { private String name; private String capital; private Currency currency; public String getName() { return name; } public void setName(String value) { this.name = value; } public String getCapital() { return capital; } public void setCapital(String value) { this.capital = value; } public Currency getCurrency() { return currency; } public void setCurrency(Currency value) { this.currency = value; } } }
3、啟動項目,使用Postman等工具進行介面測試。
四、總結
在Spring Boot中,可以很方便地實現SOAP和RESTful協議下的Web Service介面調用。選擇哪種協議,需要根據具體的需求和業務情況進行分析和選擇。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/312962.html