Spring Boot整合spring-ws开发web service

来源:互联网 发布:mastercam数控车编程 编辑:程序博客网 时间:2024/05/20 14:24

添加依赖

spring boot的工程,除了spring boot外还需要添加spring-ws和wsdl4j的依赖,当然后面生成代码还需要添加maven的jaxb2插件。

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-ws</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>wsdl4j</groupId>
  7. <artifactId>wsdl4j</artifactId>
  8. </dependency>

编写schema文件

spring-ws的发布,都是以一个schema文件(xsd)定义开始的,它描述了web service的参数以及返回的数据。

下面是官方示例给出的countries.xsd,这里我们也以它为例,当然命名空间改掉了,因为等会jaxb2插件生成代码是以它来确定包名的:

  1. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.dexcoder.com/ws"
  2. targetNamespace="http://www.dexcoder.com/ws" elementFormDefault="qualified">
  3. <xs:element name="getCountryRequest">
  4. <xs:complexType>
  5. <xs:sequence>
  6. <xs:element name="name" type="xs:string"/>
  7. </xs:sequence>
  8. </xs:complexType>
  9. </xs:element>
  10. <xs:element name="getCountryResponse">
  11. <xs:complexType>
  12. <xs:sequence>
  13. <xs:element name="country" type="tns:country"/>
  14. </xs:sequence>
  15. </xs:complexType>
  16. </xs:element>
  17. <xs:complexType name="country">
  18. <xs:sequence>
  19. <xs:element name="name" type="xs:string"/>
  20. <xs:element name="population" type="xs:int"/>
  21. <xs:element name="capital" type="xs:string"/>
  22. <xs:element name="currency" type="tns:currency"/>
  23. </xs:sequence>
  24. </xs:complexType>
  25. <xs:simpleType name="currency">
  26. <xs:restriction base="xs:string">
  27. <xs:enumeration value="GBP"/>
  28. <xs:enumeration value="EUR"/>
  29. <xs:enumeration value="PLN"/>
  30. </xs:restriction>
  31. </xs:simpleType>
  32. </xs:schema>

添加maven的jaxb2插件来生成代码

jaxb2插件可以根据描述的xsd文件来帮我们生成相应的ws代码,具体配置如下:

  1. <plugin>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-maven-plugin</artifactId>
  4. </plugin>
  5. <plugin>
  6. <groupId>org.codehaus.mojo</groupId>
  7. <artifactId>jaxb2-maven-plugin</artifactId>
  8. <version>1.6</version>
  9. <executions>
  10. <execution>
  11. <id>xjc</id>
  12. <goals>
  13. <goal>xjc</goal>
  14. </goals>
  15. </execution>
  16. </executions>
  17. <configuration>
  18. <schemaDirectory>${project.basedir}/src/main/resources//schema</schemaDirectory>
  19. <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
  20. <clearOutputDir>false</clearOutputDir>
  21. </configuration>
  22. </plugin>

配置好插件然后install一下,这样web service需要的服务端代码就已经帮我们生成好了,根据上面的xsd,生成的代码在com.dexcoder.ws包下。

生成的代码

编写Endpoint

我们就不再像spring-ws官方那样再建一个Repository了,这里直接返回。需要注意PayloadRoot注解当中的namespacelocalPart需要和xsd中对应。

  1. @Endpoint
  2. public class CountryEndpoint {
  3. private static final String NAMESPACE_URI = "http://www.dexcoder.com/ws";
  4. @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
  5. @ResponsePayload
  6. public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {
  7. GetCountryResponse response = new GetCountryResponse();
  8. Country poland = new Country();
  9. poland.setName("Poland-" + request.getName());
  10. poland.setCapital("Warsaw");
  11. poland.setCurrency(Currency.PLN);
  12. poland.setPopulation(38186860);
  13. response.setCountry(poland);
  14. return response;
  15. }
  16. }

在spring boot中配置web service

在spring boot中配置spring-ws十分简单,毕竟是同一家的东西,兼容性应该没的说。代码如下:

  1. @EnableWs
  2. @Configuration
  3. public class WebServiceConfig extends WsConfigurerAdapter {
  4. @Bean
  5. public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
  6. MessageDispatcherServlet servlet = new MessageDispatcherServlet();
  7. servlet.setApplicationContext(applicationContext);
  8. servlet.setTransformWsdlLocations(true);
  9. return new ServletRegistrationBean(servlet, "/ws/*");
  10. }
  11. @Bean(name = "countries")
  12. public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
  13. DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
  14. wsdl11Definition.setPortTypeName("CountriesPort");
  15. wsdl11Definition.setSchema(countriesSchema);
  16. return wsdl11Definition;
  17. }
  18. @Bean
  19. public XsdSchema countriesSchema() {
  20. return new SimpleXsdSchema(new ClassPathResource("schema/countries.xsd"));
  21. }
  22. }

到这里spring-ws的所有配置和工作都已经完成了,上面的DefaultWsdl11Definitionid默认就是发布的ws的访问路径。

启动项目

最后一步当然是启动项目了,这里依然使用spring boot的启动方式:

  1. @SpringBootApplication
  2. public class ApplicationStartup {
  3. public static void main(String[] args) {
  4. SpringApplication.run(ApplicationStartup.class);
  5. }
  6. }

启动后访问 http://localhost:8080/ws/countries.wsdl 发现web service已经成功发布了。

spring-ws-wsdl

这里要注意一下spring-ws发布的web service是以后缀.wsdl访问的,跟传统的?wsdl不大一样,也看过它的源码,发现是在判断后缀时写死的,所以没办法配置修改了。

还有就是spring-ws实际上把发布wsdl和真正的服务实现Endpoint分开了,如果你的Endpoint不正确,很可能会出现浏览器访问.wsdl地址看起来正常而客户端调用却出现Not Found 404的错误。

原创粉丝点击