我使用swagger-codegen-maven-plugin从schema.yaml生成Java服务器和客户端 . 但是,我无法生成propper api.json和swagger-ui.html .
访问http://localhost:8080/v2/api-docs时,我只获得API描述而不是完整的json:
{"swagger":"2.0","info":{"description":"A simple API to learn how to write OpenAPI Specification","version":"1.0.0","title":"Simple API","contact":{},"license":{}},"host":"localhost:8080","basePath":"/"}
访问http://localhost:8080/swagger-ui.html#时,出现错误页面 .
PLease帮助从yaml模式生成api-docs和swagger-ui . 从pom.xml构建:
<build>
<plugins>
<!-- Generate JAX-RS data types & client -->
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>generate-rest-client</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<language>spring</language>
<inputSpec>${project.basedir}/src/main/resources/schema.yaml</inputSpec>
<output>src/gen/java</output>
<modelPackage>${base.package}.api.model</modelPackage>
<apiPackage>${base.package}.services</apiPackage>
<invokerPackage>${base.package}.client</invokerPackage>
<addCompileSourceRoot>true</addCompileSourceRoot>
<configOptions>
<configOptions>
<apiPackage>${base.package}.services</apiPackage>
<invokerPackage>${base.package}.client</invokerPackage>
<addCompileSourceRoot>true</addCompileSourceRoot>
<sourceFolder>src</sourceFolder>
<dateLibrary>java8</dateLibrary>
<serializableModel>true</serializableModel>
</configOptions>
<sourceFolder>.</sourceFolder>
</configOptions>
</configuration>
</execution>
<execution>
<id>generate-web-ui-doc</id>
<phase>compile</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<language>html</language>
<inputSpec>${project.basedir}/src/main/resources/schema.yaml</inputSpec>
<output>src/gen/web</output>
<modelPackage>${base.package}.api.model</modelPackage>
<apiPackage>${base.package}.services</apiPackage>
<invokerPackage>${base.package}.client</invokerPackage>
<addCompileSourceRoot>true</addCompileSourceRoot>
<configOptions>
<configOptions>
<apiPackage>${base.package}.services</apiPackage>
<invokerPackage>${base.package}.client</invokerPackage>
<addCompileSourceRoot>true</addCompileSourceRoot>
<sourceFolder>src</sourceFolder>
<dateLibrary>java8</dateLibrary>
<serializableModel>true</serializableModel>
</configOptions>
<sourceFolder>.</sourceFolder>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>clean-additional-generated-files</id>
<phase>generate-sources</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
<filesets>
<fileset>
<directory>${codegen.rest.dir}</directory>
<includes>
<include>**/*Controller.java</include>
<include>**/invoker/*</include>
<include>*.*</include>
<include>io/**</include>
<include>src/**</include>
<include>.swagger-codegen/**</include>
<include>**/ApiOriginFilter.java</include>
<include>**/ApiException.java</include>
<include>**/ApiResponseMessage.java</include>
<include>**/NotFoundException.java</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</execution>
</executions>
</plugin>
<!-- Add cleaned directory with relevant Java files to the current project source paths -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${codegen.rest.dir}</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<!-- Compile generated code -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
<!--Generate api-docs page-->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<attachClasses>false</attachClasses>
<webResources>
<resource>
<directory>target</directory>
<includes>
<include>${api.json.file}</include>
</includes>
</resource>
<resource>
<directory>target</directory>
<includes>
<include>internal.json</include>
</includes>
</resource>
<resource>
<directory>target/classes</directory>
<includes>
<include>META-INF/**/*</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
schema.yaml:
swagger: "2.0"
info:version:1.0.0 title:简单API描述:一个简单的API,用于学习如何编写OpenAPI规范
方案: - http host:localhost basePath:/
paths:/ persons:get:summary:获取一些人的描述:返回一个包含所有人的列表 . 该列表支持分页 . 参数: - $ ref:“#/ parameters / pageSize” - $ ref:“#/ parameters / pageNumber”响应:200:描述:Person模式列表:$ ref:“#/ definitions / Persons”500:$ ref :“#/ responses / Standard500ErrorResponse”post:summary:创建人员描述:将新人添加到人员列表中 . 参数: - name:person in:body description:要创建的人 . schema:$ ref:“#/ definitions / Person”响应:204:description:成功创建的人员 . 400:描述:无法创建人员 . 500:$ ref:“#/ responses / Standard500ErrorResponse”
/ persons / :参数: - $ ref:“#/ parameters / username”get:summary:获取人员描述:为其用户名返回一个人 . 响应:200:描述:一个Person模式:$ ref:“#/ definitions / Person”404:$ ref:“#/ responses / PersonDoesNotExistResponse”500:$ ref:“#/ responses / Standard500ErrorResponse”delete:summary:删除一个人员描述:删除通过其用户名响应识别的单个人:204:描述:成功删除人员 . 404:$ ref:“#/ responses / PersonDoesNotExistResponse”500:$ ref:“#/ responses / Standard500ErrorResponse”
/ persons / / friends:parameters: - $ ref:“#/ parameters / username”get:summary:获取一个人的朋友描述:返回一个包含所有人的列表 . 该列表支持分页 . 参数: - $ ref:“#/ parameters / pageSize” - $ ref:“#/ parameters / pageNumber”回复:200:描述:一个人的朋友列表架构:$ ref:“#/ definitions / Persons”404:$ ref :“#/ responses / PersonDoesNotExistResponse”500:$ ref:“#/ responses / Standard500ErrorResponse”
定义:Person:required: - username properties:firstName:type:string lastName:type:string username:type:string Persons:type:array items:$ ref:“#/ definitions / Person”错误:required: - code - message properties:code:type:string message:type:string
响应:Standard500ErrorResponse:说明:发生意外错误 . schema:$ ref:“#/ definitions / Error”PersonDoesNotExistResponse:description:Person不存在 .
参数:username:name:username in:path required:true description:该人的用户名类型:string pageSize:name:pageSize in:query description:返回的人数类型:integer pageNumber:name:pageNumber in:query description:Page number类型:整数