无法让Swagger UI与Spring启动一起使用

Wim*_*uwe 21 swagger swagger-ui spring-boot

我试图让Swagger UI使用Spring Boot 1.2.1.我按照https://github.com/martypitt/swagger-springmvc上的说明操作,然后添加@EnableSwagger了我的spring配置.

我现在回到JSON http://localhost:8080/api-docs但是没有好的HTML.

我正在使用Maven并添加了对swagger-ui的依赖:

<dependency>
    <groupId>org.ajar</groupId>
    <artifactId>swagger-spring-mvc-ui</artifactId>
    <version>0.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

这是我完整的依赖列表:

<dependencies>
        <dependency>
            <groupId>com.mangofactory</groupId>
            <artifactId>swagger-springmvc</artifactId>
            <version>0.9.4</version>
        </dependency>
        <dependency>
            <groupId>org.ajar</groupId>
            <artifactId>swagger-spring-mvc-ui</artifactId>
            <version>0.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
Run Code Online (Sandbox Code Playgroud)

我也尝试http://localhost:8080/docs/index.html过URL,但这只是给出了"Whitelabel错误页面"

更新:

我在Github上创建了一个测试项目以显示问题:https://github.com/wimdeblauwe/springboot-swagger-test

小智 31

你的问题出在你的SwaggerConfiguration文件中.您需要取出@EnableWebMvc,因为这会导致默认的"SpringWebMvc"覆盖默认的Spring Boot视图解析器,它以不同的方式提供静态内容.

默认情况下,Spring Boot将提供以下任何目录中的静态内容:

  • / META-INF /资源/
  • /资源/
  • /静态的/
  • /上市/

包括webjars.

我有同样的问题,我在文档中找到了这个:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features -spring-MVC-自动配置

如果您想完全控制Spring MVC,可以添加自己的@Configuration注释@EnableWebMvc.如果你想保留Spring Boot MVC功能,而你只想添加额外的MVC配置(拦截器,格式化程序,视图控制器等),你可以添加自己@Bean的类型WebMvcConfigurerAdapter,但没有 @EnableWebMvc.

我希望这有帮助.

  • 它也在2019年帮助人们:) (4认同)