swagger传统ssm单体架构项目配置

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Author: NJ
 * @Description: swagger
 * @Date: 2019/11/2
 */
@Configuration
@EnableWebMvc
@EnableSwagger2
//必须要添加ComponentScan扫包,不添加会导致读不到注解
@ComponentScan(value="com.project.web")
public class SwaggerConfig {

    //配置文件读取是否开启
    @Value("${tool.swagger.isenable}")
    private String isEnableSwagger;

    @Bean
    public Docket createRestApi() {
        boolean flag = "true".equalsIgnoreCase(isEnableSwagger);
        System.out.println("swagger 是否开启:" + isEnableSwagger);
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .paths(PathSelectors.any())
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) // 扫描所有带ApiOperation注解的接口
                .build().enable(flag);

        return docket;
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("NJ", "" ,"");

        return new ApiInfoBuilder()
                .title("当前相项目")                         
                .description("当前相项目")         
                .contact(contact)
                .version("1.0")
                .build();
    }

}
经验分享 程序员 微信小程序 职场和发展