idea生成wsdl文件_Springboot官方例子--生成SOAP Web服务

本指南将引导您完成使用Spring创建基于SOAP的Web服务的过程,这个服务将公开公开来自不同欧洲国家的数据,供你查询。

为了简化示例,您将通过硬编码编写英国、西班牙和波兰的数据。

我利用业余时间,翻译了Spring官网的例子,方便中文不好的同学,将陆续发到头条上,欢迎大家关注,也可以上我个人BLOG:itmanclub.com,上面有已经翻译过的。

47adb2c92f58305a29e6114b860d3434.png

程序结构

└── src └── main └── java └── hello

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>4.0.0org.springframework gs-producting-web-service 0.1.0org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASEorg.springframework.boot spring-boot-starter-web 1.8org.springframework.boot spring-boot-maven-plugin 

Spring Boot将会你做如下的事:

  • 将classpath里面所有用到的jar包构建成一个可执行的 JAR 文件,方便执行你的程序
  • 搜索public static void main()方法并且将它当作可执行类
  • 根据springboot版本,去查找相应的依赖类版本,当然你可以定义其它版本。

增加Spring-WS依赖

您创建的项目需要将spring-ws-core作为依赖项包含在构建文件和wsdl4j中。

对于maven:

org.springframework.boot spring-boot-starter-web-serviceswsdl4j wsdl4j

对于gradle:

sourceCompatibility = 1.8targetCompatibility = 1.8dependencies { compile("org.springframework.boot:spring-boot-starter-web-services") testCompile("org.springframework.boot:spring-boot-starter-test") compile("wsdl4j:wsdl4j:1.6.1") jaxb("org.glassfish.jaxb:jaxb-xjc:2.2.11") compile(files(genJaxb.classesDir).builtBy(genJaxb))}

创建XML schema以定义域

Web服务域是在XML schema文件(XSD)中定义的,Spring-WS将自动导出为WSDL。

创建一个带有操作的XSD文件,以返回一个国家的名称、人口、首都和货币:

src/main/resources/countries.xsd

基于XML schema生成域类

下一步是从XSD文件生成Java类。正确的方法是在构建期间使用Maven或Gradle插件自动执行此操作。

Maven的插件配置:

org.codehaus.mojo jaxb2-maven-plugin 1.6xjcxjc${project.basedir}/src/main/resources/${project.basedir}/src/main/javafalse

生成的类放在target/generated-sources/jaxb/目录中。

Gradle的插件配置

要对Gradle执行相同的操作,首先需要在构建文件中配置JAXB:

configurations { jaxb}bootJar { baseName = 'gs-producing-web-service' version = '0.1.0' from genJaxb.classesDir}sourceCompatibility = 1.8targetCompatibility = 1.8dependencies { compile("org.springframework.boot:spring-boot-starter-web-services") testCompile("org.springframework.boot:spring-boot-starter-test") compile("wsdl4j:wsdl4j:1.6.1") jaxb("org.glassfish.jaxb:jaxb-xjc:2.2.11") compile(files(genJaxb.classesDir).builtBy(genJaxb))}

上面的构建文件有标记和结束注释,这是为了方便你了解。您自己的构建文件中不需要这些注释。

下一步是添加由gradle使用的任务genJaxb生成Java类:

task genJaxb { ext.sourcesDir = "${buildDir}/generated-sources/jaxb" ext.classesDir = "${buildDir}/classes/jaxb" ext.schema = "src/main/resources/countries.xsd" outputs.dir classesDir doLast() { project.ant { taskdef name: "xjc