首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring + OpenAPI3 when 404使用静态openApi.yaml文件

Spring + OpenAPI3 when 404使用静态openApi.yaml文件
EN

Stack Overflow用户
提问于 2022-03-31 20:58:28
回答 1查看 777关注 0票数 1

我在pom文件中添加了一个带有openAPI依赖项的简单spring应用程序设置:

代码语言:javascript
运行
复制
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.6.6</version>
    </dependency>

然后我将一个示例openapi.yaml文件放在静态资源文件夹中,然后按照文档中的说明操作:

  1. 关闭项目属性文件中的自动生成:

springdoc.api-docs.enabled=false

yaml文件放在src//

  1. /静态中,如

  1. 设置文件的swagger:

springdoc.swagger-ui.url=/myApiFile.yaml

  1. 启用最小bean配置:

@Configurationpublic类SpringDocsConfiguration { @Bean SpringDocConfiguration springDocConfiguration() {返回新的SpringDocConfiguration();} @Bean公共SpringDocConfigProperties springDocConfigProperties() {返回新的SpringDocConfiguration

当我运行应用程序并转到URL: localhost:8080/swagger-ui.html时,它显示无法获取.yaml文件:

这是我的pom.xml:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.dao.diy</groupId>
        <artifactId>ADO-Diy</artifactId>
        <version>1.0</version>
    </parent>

<!--    <groupId>org.dao.diy</groupId>-->
    <artifactId>backend</artifactId>
    <name>backend</name>
<!--    <version>0.0.1-SNAPSHOT</version>-->


    <packaging>war</packaging>

<!--    <name>backend</name>-->
    <description>backend</description>


    <properties>
        <java.version>11</java.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-data-mongodb</artifactId>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-security</artifactId>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.security</groupId>-->
<!--            <artifactId>spring-security-test</artifactId>-->
<!--            <scope>test</scope>-->
<!--        </dependency>-->

        
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.6.6</version>
        </dependency>

    </dependencies>

    <build>
      <finalName>ADO-DIY-TESTING</finalName>
        <plugins>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>./src/main/resources/static/</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>../frontend/dist/frontend/</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

        </plugins>


        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

我的调试步骤包括:

documentation

  • Googling后面的
  1. ,用于现有solutions
  2. Changing文件locations
  3. Trying不同的配置combinations
EN

回答 1

Stack Overflow用户

发布于 2022-07-19 01:35:50

确保在资源文件夹中使用以下结构:

代码语言:javascript
运行
复制
resources -> static -> myAPIFile.yaml

在这里找到答案:How to point Springdoc Swagger UI to my own YAML file instead of the auto-generated one?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71699066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档