项目原因:
最近搭建一个新的SpringBoot项目,需要配置knif4j,从其他项目里拷过来knife4j版的Swagger配置文件,结果打开doc.html显示404,如下图:
在百度一顿搜,始终没解决。官网推荐的方法是:https://doc.xiaominfo.com/guide/springboot-404.html
出错原因:
因为doc.html是在jar包里的,需要使用资源处理器注册静态资源。
复制代码:
@SpringBootApplication
@EnableWebMvc
public class TestApplication implements WebMvcConfigurer {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
问题解决