swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[Java - Spring] Leverage Boot http://start.spring.io/ when building server from swagger

Open ShawnTuatara opened this issue 10 years ago • 9 comments

Description

When creating a Spring Boot server project based on the swagger definitions I would appreciate the ability to have similar options available as is shown with spring.start.io (same is available in the Spring Eclipse Plugin when creating a new project). This reduces the manual changes I need to make to the project pom.xml later. Would also help to keep new projects up to date with the latest changes to Spring Boot.

ShawnTuatara avatar Aug 19 '16 17:08 ShawnTuatara

@ShawnTuatara Thanks for the feedback. Are you aware of the option for spring as follows?

swagger-codegen|master⚡ ⇒ java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar config-help -l spring


CONFIG OPTIONS
    sortParamsByRequiredFlag
        Sort method arguments to place required parameters before optional parameters. (Default: true)

    ensureUniqueParams
        Whether to ensure parameter names are unique in an operation (rename parameters that are not). (Default: true)

    modelPackage
        package for generated models

    apiPackage
        package for generated api classes

    invokerPackage
        root package for generated code

    groupId
        groupId in generated pom.xml

    artifactId
        artifactId in generated pom.xml

    artifactVersion
        artifact version in generated pom.xml

    sourceFolder
        source folder for generated code

    localVariablePrefix
        prefix for generated code members and local variables

    serializableModel
        boolean - toggle "implements Serializable" for generated models (Default: false)

    bigDecimalAsString
        Treat BigDecimal values as Strings to avoid precision loss. (Default: false)

    fullJavaUtil
        whether to use fully qualified name for classes under java.util. This option only works for Java API client (Default: false)

    hideGenerationTimestamp
        hides the timestamp when files were generated

    dateLibrary
        Option. Date library to use
            java8-localdatetime - Java 8 using LocalDateTime (for legacy app only)
            joda - Joda
            java8 - Java 8 native
            legacy - Legacy java.util.Date

    title
        server title name or client service name

    configPackage
        configuration package for generated code

    basePackage
        base package for generated code

    interfaceOnly
        Whether to generate only API interface stubs without the server files. (Default: false)

    singleContentTypes
        Whether to select only one produces/consumes content-type by operation. (Default: false)

    java8
        use java8 default interface (Default: false)

    async
        use async Callable controllers (Default: false)

    library
        library template (sub-template) to use (Default: spring-boot)
            spring-boot - Spring-boot Server application using the SpringFox integration.
            spring-mvc - Spring-MVC Server application using the SpringFox integration.
            spring-cloud - Spring-Cloud-Feign client with Spring-Boot auto-configured settings.

wing328 avatar Aug 22 '16 07:08 wing328

Yes I am aware but that only adds the required dependencies to have a Spring project that implements the APIs. There are a lot of cases where additional dependencies are required. It would be nice if it could have a way to define those dependencies as part of creating the the code.

ShawnTuatara avatar Aug 23 '16 06:08 ShawnTuatara

You can write a custom pom.mustache template to do that.

cbornet avatar Aug 23 '16 06:08 cbornet

To elaborate on the @cbornet answer a bit more, one can pass customized mustache templates to swagger codegen via the -t option.

wing328 avatar Aug 23 '16 06:08 wing328

That seems reasonable. I am curious if I was to use start.spring.io to build the initial project and "inject" the swagger dependencies into an existing pom, non-template, and the codegen adds the code to the project. Is that something that has been investigated? If not then the alternative would be to create a mustache template from the initial pom.xml that the start.spring.io project generates.

ShawnTuatara avatar Aug 23 '16 17:08 ShawnTuatara

@wing328 or @ShawnTuatara I've been able to configure all the generated code to go to the appropriate directories in my project, but is there a way to control the name of the SpringBootApplication runner that gets generated. Can't decide whether to use the one I created from Spring Initializr or the Swagger2SpringBoot runner that Swagger generates.

browndogg33 avatar Feb 15 '17 23:02 browndogg33

but is there a way to control the name of the SpringBootApplication runner that gets generated

Are you referring to the title option?

wing328 avatar Feb 16 '17 13:02 wing328

I don't think so. I was specifically talking about the name of the class annotated with @SpringBootApplication (essentially the main for the project). It looks like it defaults to Swagger2SpringBoot now. I was just wondering if that was configurable. If not, I can just take the one I generated from Spring Initializr and add it to the .swagger-codegen-ignore and not regenerate that class.

browndogg33 avatar Feb 16 '17 17:02 browndogg33

@browndogg33 one way is to customize the template (https://github.com/swagger-api/swagger-codegen/blob/70fa2fb78e4d0d17324bbfe4e82f4fb6970327a7/modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-boot/swagger2SpringBoot.mustache) and provide it with the -t option. Would that meet your requirement?

wing328 avatar Feb 16 '17 17:02 wing328