springboot集成swagger

1.环境

springboot:2.1.4.RELEASE

jdk:1.8

2. swagger引用

 <!-- swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!-- swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

3.swagger使用

@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket swaggerSpringMvcPlugin() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build();
    }
}

controller很多还可以分组。

@Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage(""))
                .paths(PathSelectors.any()).build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("").description("")
                .description("")
                .termsOfServiceUrl("")
                .contact(new Contact("", "", "")).version("")
                .build();
    }

多创建几个bean扫描不同的文件夹。

或者根据url分组

@Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(productApiInfo())
                .groupName("product")
                .select()
                .paths(PathSelectors.ant("/api/product/**"))
                .build();
    }

    private ApiInfo productApiInfo() {
        return new ApiInfoBuilder()
                .title("产品模块")
                .version("1.0")
                .build();
    }

4.注意事项

本人刚踩过的坑,特此记录。

swagger一定要注意路径问题。

有两种解决方法。

1.swagger配置文件和application启动文件放在一块。

这个结构不好看,放弃了。

2.使用ConfigurerAdapter配置路径。

extends WebMvcConfigurerAdapter 之后
@Configuration
public class WebMvcConfig
extends WebMvcConfigurerAdapter{
@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/"); }
}
但是这里,因为版本问题,WebMvcConfigurerAdapter被划上了过时的标志。

可以使用implements WebMvcConfigurer,WebMvcConfigurerAdapter 本来就实现了WebMvcConfigurer,现在不用中间商,直接找原厂WebMvcConfigurer.

3.使用springsecurity

使用WebSecurityConfigurerAdapter

@Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/", "/*.html", "/favicon.ico", "/statics/**");
        web.ignoring().antMatchers("/v2/api-docs/**", "/swagger-resources/**", "/webjars/**");
    }

放过swagger-ui.html一系列静态地址。

我这里为了去掉接口权限验证,加了所有请求都允许。如果只是为了验证swagger-ui.html显示的问题,这部分可以不加。

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().permitAll()
                .and()
                .csrf().disable()
                .headers().disable()
                .cors();
    }

security的引起的接口权限问题属于其他配置就不多说了。

原文地址:https://www.cnblogs.com/zhanghao1799/p/11943624.html