Spring 中的 Swagger UI:仅加载 HTML 文件,但不加载资源
Swagger UI in Spring: only HTML file is loaded, but not the resources
我正在使用 Springfox Swagger。这是我访问 http://localhost:8088/myContextRoot/swagger-ui.html:
时看到的内容
即仅 HTML 文件 swagger-ui.html
加载成功,其他内容(js、css 等)应该位于 http://localhost:8088/myContextRoot/webjars/springfox-swagger-ui returns 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"
如果您查看此 class springfox.documentation.swagger.web.ApiResourceController
,您会发现像 /configuration/ui
和 /configuration/security
这样的映射不再存在(在 io.springfox:springfox-swagger-common:2.6.1
), 因为它们分别根据 Spring 请求映射
在 /swagger-resources/configuration/ui
和 /swagger-resources/configuration/security
上更改
尝试使用这个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();
}
}
我在将 springfox-swagger2
从 2.7.0
升级到 2.8.0
时遇到了这个问题。
我通过清理 Chrome Web Browser
cookie 和会话修复了它。
我正在使用 Springfox Swagger。这是我访问 http://localhost:8088/myContextRoot/swagger-ui.html:
时看到的内容即仅 HTML 文件 swagger-ui.html
加载成功,其他内容(js、css 等)应该位于 http://localhost:8088/myContextRoot/webjars/springfox-swagger-ui returns 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"
如果您查看此 class springfox.documentation.swagger.web.ApiResourceController
,您会发现像 /configuration/ui
和 /configuration/security
这样的映射不再存在(在 io.springfox:springfox-swagger-common:2.6.1
), 因为它们分别根据 Spring 请求映射
/swagger-resources/configuration/ui
和 /swagger-resources/configuration/security
上更改
尝试使用这个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();
}
}
我在将 springfox-swagger2
从 2.7.0
升级到 2.8.0
时遇到了这个问题。
我通过清理 Chrome Web Browser
cookie 和会话修复了它。