2

我正在尝试使用 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 控制器类中导入这些生成的类。

我的理解是该<modelPackage>字段用于标识必须放置生成的类的包。我是对的吗?

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

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

4

1 回答 1

0

对于 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 链接

于 2020-10-15T13:06:11.603 回答