Search code examples
springswaggerspringfox

Swagger UI in Spring: only HTML file is loaded, but not the resources


I am using Springfox Swagger. This is what I see when I access http://localhost:8088/myContextRoot/swagger-ui.html:

enter image description here

I.e. only the HTML file swagger-ui.html is loaded successfully, other stuff (js, css, etc.) which supposed to be at http://localhost:8088/myContextRoot/webjars/springfox-swagger-ui returns 404.

What I've tried so far:

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>

resources-servlet.xml

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

Java config

@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();
         }
    }

Versions used:

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

Solution

  • If you look into this class springfox.documentation.swagger.web.ApiResourceController, you can find that mappings like /configuration/ui and /configuration/security don't exist anymore (in io.springfox:springfox-swagger-common:2.6.1), because they was changed on /swagger-resources/configuration/ui and /swagger-resources/configuration/security respectively according to Spring Request Mapping

    Try to use this Java Config:

    @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();
        }
    }