3

I am trying to use Swagger Codegen to generate model classes for my spring boot project. I found some references online and included the following plugin in my pom.xml:

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>
                            ${project.basedir}/src/main/resources/Contract-v1.yml
                        </inputSpec>
                        <generatorName>spring</generatorName>
                        <apiPackage>${project.groupId}.swagger.api</apiPackage>
                        <modelPackage>${project.groupId}.swagger.model</modelPackage>
                        <supportingFilesToGenerate>
                            ApiUtil.java
                        </supportingFilesToGenerate>
                        <configOptions>
                            <sourceFolder>src/main/java/</sourceFolder>
                            <delegatePattern>true</delegatePattern>
                            <interfaceOnly>true</interfaceOnly>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>

I run mvn install and the classes are generated in the directory /target/generated-sources/openapi. But I am unable to import these generated classes in my REST controller class.

My understanding is that the <modelPackage> field is used to identify the package in which the generated classes have to be placed. Am I right about this?

Even though these generated classes are in the right package, since they are not in the src/main/java, i am probably not able to import them in other classes.

Is there way to get these generated classes under the src/main/java directory or am I missing something in the maven config due to which these files are unavailable to other classes ?

Gurunath Sripad
  • 1,308
  • 2
  • 14
  • 24

2 Answers2

2

I also wanted to achieve what you were trying and although I fumbled initially I figured it out eventually, I did not have to do anything special. One caveat is that I used swagger-codegen-maven-plugin instead of the openapi-generator-maven-plugin.

Here is how my configuration look like on the day of latest version swagger-codegen-maven-plugin version 3.0.35. When I ran mvn compile all my classes were generated in the target folder and src main java was marked as sources folder. I was directly able to implement my own controller directly in the project by referencing the generated code API. Tbh the documentation of swagger-codegen-maven-plugin is not the best , I had to look at several places to make sense. Adding some references

General Configuration parameters: https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen-maven-plugin

Config Options: https://github.com/swagger-api/swagger-codegen/issues/7795

<plugin>
        <groupId>io.swagger.codegen.v3</groupId>
        <artifactId>swagger-codegen-maven-plugin</artifactId>
        <version>3.0.35</version>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/api-docs/swagger.yaml</inputSpec>

              <generateApis>true</generateApis>
              <generateModels>true</generateModels>
              <generateSupportingFiles>false</generateSupportingFiles>
              <language>spring</language>

              <configOptions>

                <basePackage>com.company.project</basePackage>
                <apiPackage>com.company.project.api</apiPackage>
                <modelPackage>com.company.project.model</modelPackage>

                <interfaceOnly>true</interfaceOnly>
                <sourceFolder>src/main/java</sourceFolder>
                <hideGenerationTimestamp>true</hideGenerationTimestamp>
                <dateLibrary>java11</dateLibrary>
                <ignoreUnknownJacksonAnnotation>true</ignoreUnknownJacksonAnnotation>
                <notNullJacksonAnnotation>true</notNullJacksonAnnotation>
                <dateLibrary>false</dateLibrary>
                <useBeanValidation>true</useBeanValidation>
                <serializableModel>true</serializableModel>

              </configOptions>

            </configuration>
          </execution>
        </executions>
      </plugin>
neshant sharma
  • 174
  • 2
  • 4
-1

For swagger UI you need to give the dependency and need to do some basic configurations and also in REST API you need to include couple of changes

https://blog-api-rest.herokuapp.com/swagger-ui.html#/

You can refer this API created using Swagger

In Spring boot main application file include the following code

@SpringBootApplication
@EnableSwagger2
public class BlogappApplication {

    public static void main(String[] args) {
        SpringApplication.run(BlogappApplication.class, args);
    }

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors
                        .basePackage("com.example.blogapp.controller"))
                .paths(PathSelectors.regex("/.*"))
                .build().apiInfo(apiEndPointsInfo());
    }

    private ApiInfo apiEndPointsInfo() {
        return new ApiInfoBuilder().title("Spring Boot REST API")
                .description("Blog Application REST API")
                .contact(new Contact("name", "url", "email id"))
                .build();
    }
}

And modify each and every controllers like the below code

@ApiOperation(value = "Deleting a comment", response = String.class)
    @ApiParam(value = "Comment ID", required = true)
    @ApiImplicitParam(name = "Authorization", value = "Access Token", required = true, allowEmptyValue = false, paramType = "header", dataTypeClass = String.class, example = "Bearer access_token")
    @DeleteMapping
    public String delete(@RequestParam String id){
        return commentService.delete(id);
    }

Before using the codes go through the heroku swagger link I have given