简体   繁体   English

UnitTest 调用 /swagger-ui.html 导致 springdoc-openapi 404

[英]UnitTest call /swagger-ui.html results in a 404 for springdoc-openapi

I am trying to migrate to springdoc-openapi;我正在尝试迁移到 springdoc-openapi; Everything runs great except being able to run unit-tests from mvn;除了能够从 mvn 运行单元测试之外,一切都运行良好; The following maven command results in a 404:以下 maven 命令会导致 404:

  • mvn -Dtest=SwaggerUITest test -f TestSwaggerUi -P integration mvn -Dtest=SwaggerUITest 测试 -f TestSwaggerUi -P 集成

Intellij has no problem running it, so I suspect that it is a classloading issue. Intellij 运行它没有问题,所以我怀疑这是一个类加载问题。

I have used the code from the example below and just added my unit-test我使用了下面示例中的代码,并添加了我的单元测试

Guthub code: - https://github.com/eugenp/tutorials/tree/master/spring-boot-springdoc Guthub 代码: - https://github.com/eugenp/tutorials/tree/master/spring-boot-springdoc

For the unit-test I added to the pom.xml: (I use rest-assured to check if the swagger-ui page exists)对于我添加到 pom.xml 的单元测试:(我使用 rest-assured 来检查 swagger-ui 页面是否存在)

   <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <scope>test</scope>
            <version>3.0.2</version>
    </dependency>

The test itself:测试本身:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SwaggerUITest extends AbstractRestAssuredSetupClass {

    /**
     * Test successful swagger-UI call
     **/
    @Test
    public void getSwaggerUIPage() {
        givenAnAdministratorIsAuthenticated()
                .contentType(HTML)
                .get("/swagger-ui/index.html?url=/v3/api-docs")
                .then()
                .statusCode(OK.value());
    }
}

public abstract class AbstractRestAssuredSetupClass {

    @Value("${request.logging.enabled:true}")
    private boolean logRequests;
    @Value("${response.logging.enabled:true}")
    private boolean logResponses;
    @Value("${local.server.port}")
    private int port;

    @Before
    public void setUpRestAssured() {
        RestAssured.baseURI = "http://localhost:" + port;
        RestAssured.config = config().redirect(redirectConfig().followRedirects(false));
        if (logRequests) {
            RestAssured.filters(new RequestLoggingFilter());
        }
        if (logResponses) {
            RestAssured.filters(new ResponseLoggingFilter());
        }
    }

    protected RequestSpecification givenAnAdministratorIsAuthenticated() {
        return RestAssured.given().auth().preemptive().basic("user", "user");
    }
}

I found also someone that has the same problem: https://github.com/springdoc/springdoc-openapi/issues/99我也发现有人有同样的问题: https : //github.com/springdoc/springdoc-openapi/issues/99

Unfortunately no solution.不幸的是没有解决方案。 I could also go back to springfox.我也可以回到springfox。 Issues like this caused me to migrate: https://github.com/springfox/springfox/issues/2932 .像这样的问题导致我迁移: https : //github.com/springfox/springfox/issues/2932

You can have a look, at the sample you are mentioning from baeldung.您可以查看一下您从 baeldung 中提到的示例。 We have added it to the demos witht a sample test of the UI.我们已将它添加到演示中,并进行了 UI 示例测试。 (SwaggerUnitTest). (SwaggerUnitTest)。 Its passing on travis-CI without any issue.它通过 travis-CI 没有任何问题。

You can also have a look at the tests:您还可以查看测试:

springdoc-openapi-ui升级到 1.4.6 版后问题得到解决

maybe you need to add this lines into you WebSecurityConfigs :也许您需要将此行添加到您的WebSecurityConfigs

.antMatchers("/v2/api-docs").permitAll()
.antMatchers("/swagger-ui.html").permitAll()
.antMatchers("/swagger-resources/**").permitAll()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring springdoc-openapi:swagger-ui/index.html 找不到状态=404 - Spring springdoc-openapi : swagger-ui/index.html cannot be found status=404 springdoc-openapi、swagger UI 中的 XML 示例 - XML examples in springdoc-openapi, swagger UI 使用 Spring 安全性启用 Swagger springdoc-openapi-ui (OpenAPI 3.0) - 无法访问 swagger-ui.html (401) - Enabling Swagger springdoc-openapi-ui (OpenAPI 3.0) with Spring Security - Cannot access swagger-ui.html (401) Java springdoc-openapi 在 Swagger UI 示例值中显示带有附加日期/时间字段的 LocalDateTime 字段 - Java springdoc-openapi show LocalDateTime field with additional date/time fields in Swagger UI Example Value springdoc-openapi swagger 不要带参数调用 - springdoc-openapi swagger don't make calls with parameters 尝试打开 swagger-ui.html 时出现 404 - I get 404, when trying to open swagger-ui.html /swagger-ui.html 不工作,显示 404 错误 - /swagger-ui.html is not working, it is showing 404 error springdoc-openapi 不同的例子 - springdoc-openapi different examples 如何在 spring-boot 中完全禁用 swagger-ui?(/swagger-ui.html 应该返回 404) - How to fully disable swagger-ui in spring-boot?(/swagger-ui.html should return 404) 如何使用带有 Lombok getter 的 springdoc-openapi 将 @JsonValue 用于 Swagger 枚举值 - How can @JsonValue be used for Swagger enum values using springdoc-openapi with a Lombok getter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM