【问题标题】:How to include swagger generated classes in project directory如何在项目目录中包含 swagger 生成的类
【发布时间】:2020-10-15 12:55:09
【问题描述】:

我正在尝试使用 Swagger Codegen 为我的 Spring Boot 项目生成模型类。我在网上找到了一些参考资料,并在我的 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>

我运行mvn install 并在目录/target/generated-sources/openapi 中生成类。但我无法在我的 REST 控制器类中导入这些生成的类。

我的理解是&lt;modelPackage&gt;字段是用来标识必须放置生成的类的包。我说得对吗?

即使这些生成的类在正确的包中,由于它们不在src/main/java 中,我可能无法将它们导入其他类中。

有没有办法在src/main/java 目录下获取这些生成的类,或者我是否在 maven 配置中遗漏了一些东西,因为这些文件对其他类不可用?

【问题讨论】:

    标签: java spring-boot maven swagger swagger-codegen


    【解决方案1】:

    对于 swagger UI,您需要提供依赖项并需要进行一些基本配置,并且在 REST API 中您需要包含一些更改

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

    您可以参考这个使用 Swagger 创建的 API

    在 Spring boot 主应用程序文件中包含以下代码

    @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();
        }
    }
    

    并像下面的代码一样修改每个控制器

    @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);
        }
    

    在使用代码之前,请先浏览我提供的 heroku swagger 链接

    【讨论】:

    • 您已经解释了如何为项目配置 Swagger UI。 M看如何使用swagger代码生成,
    猜你喜欢
    • 2010-11-29
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    相关资源
    最近更新 更多