学习springboot小笔记(八) ----swagger设置正常,但是页面404error的解决方法

跑别人的demo,自己加上swagger,但是却访问不了,最后发现是因为默认的路径被 @EnableWebMvc覆盖掉 或者说是 继承WebMvcConfigurerAdapter类后,默认的静态资源路径对swagger-ui.html访问不起作用,
因此要重写  WebConfigurer(继承WebMvcConfigurerAdapter的那个类) 的一个方法。

@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry)
	{
		String localPath = "file://" + imgPath;

		String osName = System.getProperty("os.name");
		//判断操作系统类型
		if(osName.toLowerCase().contains("win")){
			localPath += "/";
		}
		registry.addResourceHandler("swagger-ui.html")
				.addResourceLocations("classpath:/META-INF/resources/");

		registry.addResourceHandler("/webjars/**")
				.addResourceLocations("classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("/upload/**").addResourceLocations(localPath);

		registry.addResourceHandler("/wximg/**").addResourceLocations("file:///code/xueweishenqi/images/");
		super.addResourceHandlers(registry);
	}

然后就可以了啦~

猜你喜欢

转载自blog.csdn.net/zhang_li_ke/article/details/81411302