W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
默認情況下并不需要添加此配置即可訪問
很多朋友在使用SpringBoot集成swagger-bootstrap-ui后,都無法訪問doc.html界面,此時,你可能需要實現(xiàn)SpringBoot的WebMvcConfigurer
接口,添加相關的ResourceHandler,代碼如下:
@SpringBootApplication
public class SwaggerBootstrapUiDemoApplication implements WebMvcConfigurer{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath*:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath*:/META-INF/resources/webjars/");
}
}
或者
@SpringBootApplication
public class SwaggerBootstrapUiDemoApplication implements WebMvcConfigurer{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
如果你是使用的老的版本SpringBoot,通過繼承WebMvcConfigurationSupport
來擴展SpringBoot相關的配置,則把以上配置加在相應的addResourceHandlers
方法中即可
推薦使用實現(xiàn)WebMvcConfigurer
接口的方式來進行擴展
如果以上方式還是不行,建議開啟Spring的Debug日志來進行跟蹤,一般訪問doc.html
頁面會出現(xiàn)如下日志(成功情況下):
2019-04-19 13:39:36,896 DEBUG (AbstractHandlerMethodMapping.java:312)- Looking up handler method for path /doc.html
2019-04-19 13:39:36,902 DEBUG (AbstractHandlerMethodMapping.java:322)- Did not find handler method for [/doc.html]
2019-04-19 13:39:36,921 DEBUG (AbstractUrlHandlerMapping.java:199)- Matching patterns for request [/doc.html] are [/**]
2019-04-19 13:39:36,922 DEBUG (AbstractUrlHandlerMapping.java:233)- URI Template variables for request [/doc.html] are {}
2019-04-19 13:39:36,923 DEBUG (AbstractUrlHandlerMapping.java:146)- Mapping [/doc.html] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@da32f3]]] and 1 interceptor
2019-04-19 13:39:36,957 DEBUG (RequestContextFilter.java:104)- Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@28759ea7
2019-04-19 13:39:41,649 DEBUG (RequestContextFilter.java:114)- Bound request context to thread: org.apache.catalina.connector.RequestFacade@28759ea7
同理,在使用SpringMvc或者shiro等權限框架時,如果頁面無法訪問,配置doc.html屬性即可
關于SpringBoot的代碼示例可參考swagger-bootstrap-ui-demo
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: