How to include swagger generated classes in project directory

Viewed 1799

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 ?

1 Answers

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

Related