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服务相关推荐
- Spring Boot文档阅读笔记-构建SOAP的web Service Client
同样,这里是使用Maven及Java8 同样Maven添加如下依赖: <dependency><groupId>wsdl4j</groupId><artifa ...
- Spring Boot文档阅读笔记-构建Restful风格的WebService客户端
对应的maven如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- Spring Boot文档阅读笔记-构建Restful风格的WebService
Maven代码如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- Spring Boot文档阅读笔记-对Securing a Web Application解析
首先创建一个非安全的Web应用 这个应用包含两个页面,一个是home页面,一个是"Hello,World"页面.home页面使用Thymeleaf,相关代码如下: <!DOC ...
- Spring Boot文档阅读笔记-how-to-implement-2-way-ssl-using-spring-boot
two-way-ssl需要12次握手(除去TCP的三次握手),如下图: 双向认证过程: 1.客户端发送ClientHello消息,告诉服务端要使用SSL. 2.客户端发送ServerHello的响应, ...
- Spring Boot文档阅读笔记-EhCache的使用
这里要先注意2个概念: buffer和cache,很多人会讲这两个概念混用.但其实这是两个概念! buffer:一般是指存储临时数据的实体.只能读写一次,对于程序员来说buffer是可见的,比如TCB ...
- Spring Boot文档阅读笔记-Spring Boot @Bean解析
利用SpringBoot的@Bean创建一个简单的Bean. Spring的@Bean注解是放在方法上的,带上这个注解的方法会被Spring容器管理.并且这个方法要返回一个值(对象),这个值和对象会被 ...
- Spring Boot文档阅读笔记-对Messaging with RabbitMQ解析
此篇教程以Rabbitmq作为消息队列服务端,使用Spring Boot产生和发布消息. 使用Spring AMQP的RabbitTemplate发布消息,使用MessageListenerAdapt ...
- Spring Boot文档阅读笔记-@SpringBootApplication官方解析与实例(1.5.19)
目录 官方解析 博主例子 官方解析 @SpringBootApplication有如下3个特点: 1. @EnableAutoConfiguration: 能够启动Spring Boot的自动配置机制 ...
最新文章
- C++——智能指针——auto_ptr、shared_ptr、unique_ptr
- Java不同场景加载不同类_[改善Java代码]不同的场景使用不同的泛型通配符
- hdu 3549 Flow Problem(最大流模板题)
- 用“谬论”指挥研究方向数十年,是谁让“老年痴呆”至今仍是绝症?
- echarts 中 symbol 自定义图片
- 飞船向上飞pygame用k_up_十分钟就能用Python教你开发出一个迷你打飞机的游戏
- 在线YAML转XML工具
- 上海java工作经验与薪资_Java硕士京东工作1年,跳槽后他期望薪资26K,大家感觉他可以吗...
- 他们每天都在为梦想排除万难,那你呢?
- 关于软考的一些事,你知道吗?
- 企业员工人事管理系统(数据库课设)
- SpringCound-Alibaba
- 定时器的用法以及pwm的调速
- 阿里P4 - P14技能要求及对应薪资曝光
- Kafka生产者、消费者的消息可靠性方案实现
- 接口用例设计从哪些方面考虑
- Ansys2020R2的Fluent网格重排问题(reorder)
- 中国IT女性生存状态写实(转)
- NGUI图集分解 切割
- 台式计算机的日常保养,台式电脑怎么保养