javascript
Spring Boot文档阅读笔记-构建SOAP的web Service服务
這里使用的Maven,Java 8來操作的。
Maven相關代碼為:
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>wsdl4j</groupId><artifactId>wsdl4j</artifactId><version>1.6.1</version></dependency><dependency><groupId>org.springframework.ws</groupId><artifactId>spring-ws-core</artifactId><version>3.0.8.RELEASE</version></dependency></dependencies>buid里面要添加一個插件:
<build><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>2.5.0</version><executions><execution><id>xjc</id><goals><goal>xjc</goal></goals></execution></executions><configuration><sources><source>${project.basedir}/src/main/resources/countries.xsd</source></sources></configuration></plugin></plugins></build>這里要創建一個xml,通過這個xml生成對應的Object
這里countries.xsd為:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://it1995.com/example/demo"targetNamespace="http://it1995.com/example/demo" 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>其中target/jaxb/com/it1995/example/demo是這樣生成的:
其中這個文件路徑是xsd文件xmlns:tns,targetNamespace
xmlns:tns和targetNamespace生成對應的包。
下面是關于各個文件的分
析:
CountryRepository.java:存儲城市的數據,為webService提供數據。
?
CountryEndpoint.java:創建服務端,需要使用到POJO類及少量的Spring WS注解處理SOAP的請求。
其中:
其中NAMESPACE_URL為XML里面的xmlns:tns,targetNamespace。其中里面涉及幾個注解:
@Endpoint:將此類注冊為Spring WS的候選類,用于接收SOAP消息;
@PayloadRoot:這個也是Spring WS的注解作用在方法上其中namespace填寫xml的namespace,而localpart填寫xml中作用的方法;
@RequestPayload:方法中參數的注解,將SOAP傳過來的數據映射到參數中;
@ResponsePayload:返回響應數據。
?
WebServiceConfig.java:配置Spring WS相關的配置Bean
@EnableWs @Configuration public class WebServiceConfig extends WsConfigurerAdapter {@Beanpublic 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://it1995.com/example/demo");wsdl11Definition.setSchema(countriesSchema);return wsdl11Definition;}@Beanpublic XsdSchema countriesSchema() {return new SimpleXsdSchema(new ClassPathResource("countries.xsd"));} }Spring WS有自己的servlet用于處理SOAP消息。上面的MessageDispatcherServlet十分重要,他將ApplicatoinContext注入到了MessageDispatcherServlet。并且這個Bean并不會影響Spring 的默認Bean。
?
程序運行截圖如下:
源碼打包下載地址:
https://github.com/fengfanchen/Java/tree/master/SOAPWebProduction
?
?
?
總結
以上是生活随笔為你收集整理的Spring Boot文档阅读笔记-构建SOAP的web Service服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android逆向笔记-IDA Pro动
- 下一篇: gradle idea java ssm