【问题标题】:Swagger UI in Spring: only HTML file is loaded, but not the resourcesSpring中的Swagger UI:仅加载HTML文件,但不加载资源
【发布时间】:2017-06-23 10:25:08
【问题描述】:

我正在使用 Springfox Swagger。这是我访问http://localhost:8088/myContextRoot/swagger-ui.html时看到的:

即只有 HTML 文件 swagger-ui.html 加载成功,其他应该在 http://localhost:8088/myContextRoot/webjars/springfox-swagger-ui 的东西(js、css 等)返回 404。

到目前为止我已经尝试过:

web.xml

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/swagger-ui.html</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/webjars/**</url-pattern>
</servlet-mapping>

资源-servlet.xml

<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />

Java 配置

@Configuration
@EnableWebSecurity
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
         @Override
         protected void configure(HttpSecurity http) throws Exception {
              http
                .authorizeRequests()
                .antMatchers("/v2/api-docs", "/configuration/ui/**", "/swagger-resources", "/configuration/security/**",
                        "/webjars/**", "/swagger-ui.html")
                .permitAll();
         }
    }

使用的版本:

compile "io.springfox:springfox-swagger2:2.6.1"
compile "io.springfox:springfox-swagger-ui:2.6.1"

【问题讨论】:

    标签: spring swagger springfox


    【解决方案1】:

    如果您查看此类 springfox.documentation.swagger.web.ApiResourceController,您会发现 /configuration/ui/configuration/security 之类的映射不再存在(在 io.springfox:springfox-swagger-common:2.6.1 中),因为它们在 /swagger-resources/configuration/ui/swagger-resources/configuration/security 上已更改分别根据Spring Request Mapping

    尝试使用这个 Java 配置:

    @Configuration
    @EnableWebSecurity
    @EnableWebMvcSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                .antMatchers("/v2/api-docs", "/swagger-resources/**", "/webjars/**", "/swagger-ui.html")
                .permitAll();
        }
    }
    

    【讨论】:

      【解决方案2】:

      我在将springfox-swagger22.7.0升级到2.8.0时遇到了这个问题。

      我只是通过清理Chrome Web Browser cookie 和会话来修复它。

      【讨论】:

        猜你喜欢
        • 2018-01-29
        • 2018-03-26
        • 2012-11-21
        • 1970-01-01
        • 1970-01-01
        • 2020-08-28
        • 2021-09-01
        • 1970-01-01
        • 2017-02-25
        相关资源
        最近更新 更多