SpringBoot訪問(wèn)doc.html頁(yè)面404

2019-05-30 20:55 更新

默認(rèn)情況下并不需要添加此配置即可訪問(wèn)

很多朋友在使用SpringBoot集成swagger-bootstrap-ui后,都無(wú)法訪問(wèn)doc.html界面,此時(shí),你可能需要實(shí)現(xiàn)SpringBoot的WebMvcConfigurer接口,添加相關(guān)的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,通過(guò)繼承WebMvcConfigurationSupport來(lái)擴(kuò)展SpringBoot相關(guān)的配置,則把以上配置加在相應(yīng)的addResourceHandlers方法中即可

推薦使用實(shí)現(xiàn)WebMvcConfigurer接口的方式來(lái)進(jìn)行擴(kuò)展

如果以上方式還是不行,建議開(kāi)啟Spring的Debug日志來(lái)進(jìn)行跟蹤,一般訪問(wèn)doc.html頁(yè)面會(huì)出現(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等權(quán)限框架時(shí),如果頁(yè)面無(wú)法訪問(wèn),配置doc.html屬性即可

關(guān)于SpringBoot的代碼示例可參考swagger-bootstrap-ui-demo

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)