Spring Boot整合spring-ws开发web service
来源:互联网 发布:mastercam数控车编程 编辑:程序博客网 时间:2024/05/20 14:24
添加依赖
spring boot的工程,除了spring boot外还需要添加spring-ws和wsdl4j的依赖,当然后面生成代码还需要添加maven的jaxb2插件。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-ws</artifactId>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>
编写schema文件
spring-ws的发布,都是以一个schema
文件(xsd)定义开始的,它描述了web service的参数以及返回的数据。
下面是官方示例给出的countries.xsd
,这里我们也以它为例,当然命名空间改掉了,因为等会jaxb2插件生成代码是以它来确定包名的:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.dexcoder.com/ws"
targetNamespace="http://www.dexcoder.com/ws" 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>
添加maven的jaxb2插件来生成代码
jaxb2插件可以根据描述的xsd文件来帮我们生成相应的ws代码,具体配置如下:
<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//schema</schemaDirectory>
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</plugin>
配置好插件然后install
一下,这样web service需要的服务端代码就已经帮我们生成好了,根据上面的xsd,生成的代码在com.dexcoder.ws
包下。
编写Endpoint
我们就不再像spring-ws官方那样再建一个Repository了,这里直接返回。需要注意PayloadRoot
注解当中的namespace
和localPart
需要和xsd中对应。
@Endpoint
public class CountryEndpoint {
private static final String NAMESPACE_URI = "http://www.dexcoder.com/ws";
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
@ResponsePayload
public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {
GetCountryResponse response = new GetCountryResponse();
Country poland = new Country();
poland.setName("Poland-" + request.getName());
poland.setCapital("Warsaw");
poland.setCurrency(Currency.PLN);
poland.setPopulation(38186860);
response.setCountry(poland);
return response;
}
}
在spring boot中配置web service
在spring boot中配置spring-ws十分简单,毕竟是同一家的东西,兼容性应该没的说。代码如下:
@EnableWs
@Configuration
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.setSchema(countriesSchema);
return wsdl11Definition;
}
@Bean
public XsdSchema countriesSchema() {
return new SimpleXsdSchema(new ClassPathResource("schema/countries.xsd"));
}
}
到这里spring-ws的所有配置和工作都已经完成了,上面的DefaultWsdl11Definition
id默认就是发布的ws的访问路径。
启动项目
最后一步当然是启动项目了,这里依然使用spring boot的启动方式:
@SpringBootApplication
public class ApplicationStartup {
public static void main(String[] args) {
SpringApplication.run(ApplicationStartup.class);
}
}
启动后访问 http://localhost:8080/ws/countries.wsdl 发现web service已经成功发布了。
这里要注意一下spring-ws发布的web service是以后缀.wsdl
访问的,跟传统的?wsdl
不大一样,也看过它的源码,发现是在判断后缀时写死的,所以没办法配置修改了。
还有就是spring-ws实际上把发布wsdl
和真正的服务实现Endpoint
分开了,如果你的Endpoint
不正确,很可能会出现浏览器访问.wsdl
地址看起来正常而客户端调用却出现Not Found 404的错误。
- Spring Boot整合spring-ws开发web service
- Spring Boot整合spring-ws调用web service服务
- Spring boot 整合CXF开发web service
- Spring boot 整合CXF开发web service
- Spring boot 整合CXF开发web service
- Spring boot 整合CXF开发web service
- Spring Boot、Mybatis框架整合开发Java RESTful Web Service
- WEB开发----spring boot与mybatis整合
- 使用Spring WS 2.0开发基于SOAP的web service
- JAX-WS 和 Spring 整合开发步骤:
- JAX-WS整合Spring
- Spring整合JAX-WS
- 使用Axis开发Web Service程序 整合 spring
- 使用Axis开发Web Service程序整合 spring
- Spring Boot 整合 Thymeleaf 完整 Web 案例
- Spring Boot 整合 Thymeleaf 完整 Web 案例
- Spring Boot的Web开发
- spring boot开发web应用
- 前端项目配置启动 node +vue +es6
- Windows驱动安装info和调试
- Unity Profiler 性能分析
- C++使用正则表达式
- reids安装
- Spring Boot整合spring-ws开发web service
- Android中获取App的File路径,以及拷贝Asses目录下的文件
- 第六天 JSON和AJAX基础教程
- ElasticSearch 基本查询语法
- eclipse 使用maven出现build path entyies are missing
- activity_main
- 自定义仿京东加减图标
- 剑指offer 37 两个链表的第一个公共结点
- Docker Hello World