最近项目访问swagger-ui.html时突然访问不了,后来发现由于集成了thymeleaf,配置了模板路径前缀,而swagger的路径是
/swagger-ui.html,没有前缀,所有会出现访问不了问题。
解决:
在WebMvcConfigurer实现类里面加上以下代码即可:
代码:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @Author: gourd
* @Date: 2018/9/19 15:40
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/login.html").setViewName("login");
registry.addViewController("/websocket.html").setViewName("websocket");
registry.addViewController("/error.html").setViewName("error");
registry.addViewController("/file.html").setViewName("file");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}