关于 springboot 整合swagger遇到的大坑问题,意想不到

配置好后访问sawgger-ui出现404,首先清理缓存!! 坑了我一下午

 

1.首先加入相关依赖

        
			io.springfox
			springfox-swagger2
			2.7.0
		
		
			io.springfox
			springfox-swagger-ui
			2.7.0
		

2.生成一个SwaggerConfig配置文件

@Configuration   //不可缺少 相当于spring xml配置中的bean,放置在容器中
@EnableSwagger2  // 启动类注解
public class SwaggerConfig {
    @Bean
    public Docket swaggerSpringMvcPlugin() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("business-api")
                .select()
                .paths(PathSelectors.any())
                .apis(RequestHandlerSelectors.basePackage("com.zy.springboot.Controller")) 
      //扫描Controller
                .build();
    }
/**
     * swagger api 信息描述
* @return {@link ApiInfo swagger API 对象} */ private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("系统 API") .description("了解更多信息:http://www.baidu.com/") .termsOfServiceUrl("http://blog.didispace.com/") .license("License Version 1.0") .contact("xx开发组") .version("1.0") .build(); } }

3.先跳过,如果配置完成后访问404再加入,映射资源文件用

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

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

4. 启动类   要和控制器所有的包同级,放在最外边

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

5.启动 。访问http://localhost:8080/swagger-ui.html

 

 

你可能感兴趣的:(关于 springboot 整合swagger遇到的大坑问题,意想不到)