Spring Boot整合webservice
阿新 • • 發佈:2019-02-16
一:概念
Web Service平臺需要一套協議來實現分散式應用程式的建立。任何平臺都有它的資料表示方法和型別系統。要實現互操作性,Web Service平臺必須提供一套標準的型別系統,用於溝通不同平臺、程式語言和元件模型中的不同型別系統。這些協議有: XML和XSD 可擴充套件的標記語言(標準通用標記語言下的一個子集)是Web Service平臺中表示資料的基本格式。除了易於建立和易於分析外,XML主要的優點在於它既與平臺無關,又與廠商無關。XML是由全球資訊網協會(W3C)建立,W3C制定的XML SchemaXSD 定義了一套標準的資料型別,並給出了一種語言來擴充套件這套資料型別二:springboot整合
1.新增maven依賴<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Compile --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> </dependency> <dependency> <groupId>org.jdom</groupId> <artifactId>jdom2</artifactId> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> </dependency>
<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
<!-- 生成實體類外掛-->
<plugin>
<groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>1.6</version> <executions> <execution> <id>xjc</id> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration>
<schemaDirectory>${project.basedir}/src/main/resources/</schemaDirectory> <outputDirectory>${project.basedir}/src/main/java</outputDirectory> <clearOutputDir>false</clearOutputDir> </configuration> </plugin></plugins>
2.新增XML Schema定義資料結構,檔名為countries.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://wind.com/webservices/service" targetNamespace="http://wind.com/webservices/service" elementFormDefault="qualified"> <xs:element name="getCountryRequest"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="getCountryResponse"> <xs:complexType> <xs:sequence> <xs:element name="country" type="tns:country"/> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="country"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="population" type="xs:int"/> <xs:element name="capital" type="xs:string"/> <xs:element name="currency" type="tns:currency"/> </xs:sequence> </xs:complexType> <xs:simpleType name="currency"> <xs:restriction base="xs:string"> <xs:enumeration value="GBP"/> <xs:enumeration value="EUR"/> <xs:enumeration value="PLN"/> </xs:restriction> </xs:simpleType> </xs:schema>
3.生成實體類,在當前專案根目錄下執行mvn clean install
生成路徑為
com.wind.webservices.service,跟上面
targetNamespace="http://wind.com/webservices/service"對應
4.建立一個Repository
@Component public class CountryRepository { private static final Map<String, Country> countries = new HashMap<>(); @PostConstruct public void initData() { Country spain = new Country(); spain.setName("Spain"); spain.setCapital("Madrid"); spain.setCurrency(Currency.EUR); spain.setPopulation(46704314); countries.put(spain.getName(), spain); Country poland = new Country(); poland.setName("Poland"); poland.setCapital("Warsaw"); poland.setCurrency(Currency.PLN); poland.setPopulation(38186860); countries.put(poland.getName(), poland); Country uk = new Country(); uk.setName("United Kingdom"); uk.setCapital("London"); uk.setCurrency(Currency.GBP); uk.setPopulation(63705000); countries.put(uk.getName(), uk); } public Country findCountry(String name) { Assert.notNull(name, "The country's name must not be null"); return countries.get(name); } }5.建立一個服務EndPoint
@Endpoint public class CountryEndpoint {
//跟countries.xsd檔案中得保持一致
private static final String NAMESPACE_URI = "http://wind.com/webservices/service";private CountryRepository countryRepository;@Autowiredpublic CountryEndpoint(CountryRepository countryRepository) { this.countryRepository = countryRepository;}
//配置對外介面 @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest") @ResponsePayload public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) { GetCountryResponse response = new GetCountryResponse(); response.setCountry(countryRepository.findCountry(request.getName())); return response; } }6.新增webservice配置
@Configuration @EnableWs public class WebServiceConfig extends WsConfigurerAdapter { @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://wind.com/webservices/service"); wsdl11Definition.setSchema(countriesSchema); return wsdl11Definition; } @Bean public XsdSchema countriesSchema() { return new SimpleXsdSchema(new ClassPathResource("countries.xsd")); } }7.啟動應用
@SpringBootApplication public class SampleWebServicesApplication { public static void main(String[] args) throws Exception { SpringApplication.run(SampleWebServicesApplication.class, args); } }在瀏覽器中訪問:http://localhost:8080/ws/countries.wsdl 。
8.傳送SOAP請求
定義請求檔案
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:gs="http://wind.com/webservices/service"> <soapenv:Header/> <soapenv:Body> <gs:getCountryRequest> <gs:name>Spain</gs:name> </gs:getCountryRequest> </soapenv:Body> </soapenv:Envelope>傳送curl請求(window上可以用SoapUI工具,unix/linux可以直接用命令列)
curl --header "content-type: text/xml" -d @request.xml http://localhost:8080/ws
或者HttpURLConnection傳送請求
InputStream is = null; OutputStream os = null; HttpURLConnection conn = null; //服務的地址 try { URL wsUrl = new URL("http://localhost:8080/ws"); conn = (HttpURLConnection) wsUrl.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); os = conn.getOutputStream(); //請求體 String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" + " xmlns:gs=\"http://wind.com/webservices/service\">\n" + " <soapenv:Header/>\n" + " <soapenv:Body>\n" + " <gs:getCountryRequest>\n" + " <gs:name>Spain</gs:name>\n" + " </gs:getCountryRequest>\n" + " </soapenv:Body>\n" + "</soapenv:Envelope>"; os.write(soap.getBytes()); is = conn.getInputStream(); byte[] b = new byte[1024]; int len = 0; String s = ""; while((len = is.read(b)) != -1){ String ss = new String(b,0,len,"UTF-8"); s += ss; } System.out.println(s); } catch (IOException e) { e.printStackTrace(); } finally { if(is != null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(os != null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(conn != null){ conn.disconnect(); } }