Gradle Swagger CodeGen默认生成器CodegenConfigurator添加Lombok

编程入门 行业动态 更新时间:2024-10-22 22:51:58
本文介绍了Gradle Swagger CodeGen默认生成器CodegenConfigurator添加Lombok的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有个招摇和下面的build.gradle文件

I have a swagger and the following build.gradle file

buildscript { ext { springBootVersion = '2.1.8.RELEASE' } repositories { maven { url "mavenrepo.schwab/nexus/content/groups/public" } maven { url "mavenrepo.schwab/nexus/content/repositories/releases/" } } dependencies { classpath("io.swagger:swagger-codegen:2.4.7") classpath "io.spring.gradle:dependency-management-plugin:1.0.8.RELEASE" classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7" } } apply plugin: 'idea' apply plugin: 'java' apply plugin: 'maven' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' repositories { maven { url "mavenrepo.schwab/nexus/content/groups/public" } } import io.swagger.codegen.DefaultGenerator import io.swagger.codegen.config.CodegenConfigurator def swaggerInput = "${rootDir}/api/POMOrchestrator.v1.json" def swaggerOutputDir = file('application/') task generateApi { inputs.file(swaggerInput) outputs.dir(swaggerOutputDir) doLast { def config = new CodegenConfigurator() config.setInputSpec(swaggerInput) config.setOutputDir(swaggerOutputDir.path) config.setIgnoreFileOverride(swaggerOutputDir.path) config.setLang('spring') config.setAdditionalProperties([ 'invokerPackage': 'com.schwab.brokerage.party.onborading', 'modelPackage' : 'com.schwab.brokerage.party.onborading.models.swagger', 'apiPackage' : 'com.schwab.brokerage.party.onborading.api.inbound.rest.controller', 'dateLibrary' : 'java8', 'useTags' : 'true', // Use tags for the naming 'interfaceOnly' : 'true' // Generating the Controller API interface and the models only ]) config.setImportMappings([ 'hello': 'com.schwab.cat.onbording.model.Hello' ]) new DefaultGenerator().opts(config.toClientOptInput()).generate() } } clean.doFirst { delete(swaggerOutputDir) } configurations { swagger } sourceSets { swagger { compileClasspath = configurations.swaggerCompile java { srcDir file("${project.buildDir.path}/swagger/src/main/java") } } main { compileClasspath += swagger.output runtimeClasspath += swagger.output } test { compileClasspath += swagger.output runtimeClasspath += swagger.output } } compileSwaggerJava.dependsOn generateApi classes.dependsOn swaggerClasses compileJava.dependsOn compileSwaggerJava ext { springBootVersion = '2.1.8.RELEASE' swaggerVersion = '2.7.0' springCloudServicesVersion = '2.1.2.RELEASE' springCloudVersion = 'Greenwich.RC1' cucumberVersion = '2.3.1' jackson_version = '2.4.2' jersey_version = '1.18' jodatime_version = '2.3' junit_version = '4.8.1' } dependencies { swaggerCompile "org.springframework.boot:spring-boot-starter-web:$springBootVersion" swaggerCompile 'io.swagger:swagger-annotations:1.5.16' swaggerCompile 'com.squareup.okhttp:okhttp:2.7.5' swaggerCompile 'com.squareup.okhttp:logging-interceptor:2.7.5' swaggerCompile 'com.google.code.gson:gson:2.8.1' compile sourceSets.swagger.output compile "com.sun.jersey:jersey-client:$jersey_version" compile "com.sun.jersey.contribs:jersey-multipart:$jersey_version" compile "com.fasterxml.jackson.core:jackson-core:$jackson_version" compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version" compile "com.fasterxml.jackson.core:jackson-databind:$jackson_version" compile "com.fasterxml.jackson.datatype:jackson-datatype-joda:2.1.5" compile "joda-time:joda-time:$jodatime_version" compile 'io.swagger:swagger-codegen:2.2.3' testCompile "junit:junit:$junit_version" runtime 'com.squareup.okhttp:okhttp:2.7.5' runtime 'com.squareup.okhttp:logging-interceptor:2.7.5' runtime 'com.google.code.gson:gson:2.8.1' }

这对于创建没有lombok批注的模型非常有用,对于我来说,能够在顶部添加@Data和@Builder确实很有帮助.有没有办法告诉代码生成器添加这些(也许是设置)?

This does a fine job of creating the models only they do not have lombok annotations and for my purposes it would be really helpful to be able to add @Data and @Builder at the top. Is there a way to tell the code gen to add these (perhaps a setting)?

推荐答案

解决方案是使用胡须(请参见 github/spullara/mustache.java ).这是添加了龙目岛注释的pojo胡子.

The solution was to use Mustaches (see github/spullara/mustache.java). Here is the pojo mustache that added the lombok annotation.

import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import lombok.Getter; /** * {{#description}}{{.}}{{/description}}{{^description}}{{classname}}{{/description}} */{{#description}} @ApiModel(description = "{{{description}}}"){{/description}} {{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}{{>additionalModelTypeAnnotations}} @Data @Builder @NoArgsConstructor @AllArgsConstructor public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}}{{^parent}}{{#hateoas}}extends RepresentationModel<{{classname}}> {{/hateoas}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} { {{#serializableModel}} private static final long serialVersionUID = 1L; {{/serializableModel}} {{#vars}} {{#isEnum}} {{^isContainer}} {{>enumClass}} {{/isContainer}} {{#isContainer}} {{#mostInnerItems}} {{>enumClass}} {{/mostInnerItems}} {{/isContainer}} {{/isEnum}} {{#jackson}} @JsonProperty("{{baseName}}"){{#withXml}} @JacksonXmlProperty({{#isXmlAttribute}}isAttribute = true, {{/isXmlAttribute}}{{#xmlNamespace}}namespace="{{xmlNamespace}}", {{/xmlNamespace}}localName = "{{#xmlName}}{{xmlName}}{{/xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}"){{/withXml}} {{/jackson}} {{#gson}} @SerializedName("{{baseName}}") {{/gson}} {{#isContainer}} {{#useBeanValidation}}@Valid{{/useBeanValidation}} private {{>nullableDataType}} {{name}}; {{/isContainer}} {{^isContainer}} {{#isDate}} @org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE) {{/isDate}} {{#isDateTime}} @org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME) {{/isDateTime}} private {{>nullableDataType}} {{name}}; {{/isContainer}} {{/vars}} /** * Convert the given object to string with each line indented by 4 spaces * (except the first line). */ private String toIndentedString(java.lang.Object o) { if (o == null) { return "null"; } return o.toString().replace("\n", "\n "); } }

更多推荐

Gradle Swagger CodeGen默认生成器CodegenConfigurator添加Lombok

本文发布于:2023-11-17 12:02:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609937.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:生成器   Swagger   Gradle   CodeGen   Lombok

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!