Spring boot集成knife4j

2022-11-02 15:58:23 浏览数 (1)

1. 引入依赖

代码语言:javascript复制
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.7</version>
</dependency>

2. 配置config

代码语言:javascript复制
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * @author 
 * @title: Knife4jConfiguration
 * @projectName 
 * @description: TODO
 * @date 2022/3/21 0021下午 13:55
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //.title("swagger-bootstrap-ui-demo RESTful APIs")
                        .description("# swagger-bootstrap-ui-demo RESTful APIs")
                        .termsOfServiceUrl("http://www.xxx.com/")
                        .contact("1251767662@qq.com")
                        .version("1.0")
                        .build())
                //分组名称
                .groupName("2.X版本")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.springbootknife4jintegrate.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
}

3. 配置Controller

代码语言:javascript复制
import com.yinbao.data.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@Api(tags = "推送模块")
@RestController
@RequestMapping("/push/api")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiImplicitParam(name = "token",value = "令牌",required = true)
    @ApiOperation(value = "推送")
    @RequestMapping("/itemSync")
    public void itemSync(@RequestParam(value = "token")String token){
        log.info(token);
    }

    @ApiOperation(value = "全部推送")
    @RequestMapping("/allItemSync")
    public void allItemSync(){
        orderService.allItemSync();
    }
}

访问地址:http://localhost:8099/doc.html#/home

 如果配置了拦截器将swaager的请求放开

代码语言:javascript复制
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(new AdminInterceptor());
        registration.addPathPatterns("/**");                      //所有路径都被拦截
        registration.excludePathPatterns(                         //添加不拦截路径
                "/swagger**/**",            //swaager
                "/**/*.html",            //html静态资源
                "/**/*.js",              //js静态资源
                "/**/*.css",             //css静态资源
                "/**/*.woff",
                "/**/*.ttf"
        );
    }

4. 增强特性

配置登陆访问限制

代码语言:javascript复制
knife4j:
  enable: true
  # 开启Swagger的Basic认证功能,默认是false
  basic:
    enable: true
    # Basic认证用户名
    username: admin
    # Basic认证密码
    password: 1qaz2wsx

效果:会要求输入配置好的用户名密码

如果需要关闭对外文档,如下配置

代码语言:javascript复制
knife4j:
  enable: true
    #  屏蔽接口文档 无法访问接口 /doc.html
  production: true

0 人点赞