如何在Spring Boot中生成swagger uu +模型? Spring Boot 和 swagger 的 gradle 文件示例。从文件生成模型。
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath('io.swagger.codegen.v3:swagger-codegen-maven-plugin:3.0.52')
}
}
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.2'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
import io.swagger.codegen.v3.CodegenConfigLoader
import io.swagger.codegen.v3.DefaultGenerator
import io.swagger.codegen.v3.ClientOptInput
import io.swagger.codegen.v3.ClientOpts
import io.swagger.v3.parser.OpenAPIV3Parser
ext.apiPackage = 'com.api'
ext.modelPackage = 'com.model'
java {
sourceCompatibility = '17'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
implementation 'io.swagger.codegen.v3:swagger-codegen-maven-plugin:3.0.52'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
task generateServer {
doLast {
def openAPI = new OpenAPIV3Parser().read("${rootDir}/src/main/resources/spec.yml", null, null)
def clientOpts = new ClientOptInput().openAPI(openAPI)
def codegenConfig = CodegenConfigLoader.forName('spring')
//codegenConfig.setOutputDir(project.buildDir.toString())
codegenConfig.setOutputDir(project.buildDir.toString())
clientOpts.setConfig(codegenConfig)
def clientOps = new ClientOpts()
clientOps.setProperties([
'dateLibrary' : 'java8', // Date library to use
'useTags' : 'true', // Use tags for the naming
'interfaceOnly' : 'true' // Generating the Controller API interface and the models only
])
clientOpts.setOpts(clientOps)
def generator = new DefaultGenerator().opts(clientOpts)
generator.generate() // Executing the generation
}
}