springboot整合knife4j,swagger


springboot整合knife4j,swagger

1、添加依赖

 
      
        org.springframework.boot
        spring-boot-starter-web
      

      
        com.github.xiaoymin
        knife4j-spring-boot-starter
        2.0.7
      

    

2、添加配置文件

package com.hg.cosmo.neo4j.data.configure;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
* @author liubh
*/
@Configuration
@EnableSwagger2WebMvc
/**
* 通过配置文件是否开启swagger,生成环境一般不开启
*/
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfigure {
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
ParameterBuilder tokenPar = new ParameterBuilder();
List pars = new ArrayList<>();
tokenPar.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
pars.add(tokenPar.build());

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.xx.com/")
.version("1.0")
.build())
//分组名称
.groupName("2.X版本")
.globalOperationParameters(pars)
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
return docket;
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("Spring Boot Swagger2 构建RESTful API")
//条款地址
.termsOfServiceUrl("http://localhost:8081/")
.version("1.0")
//描述
.description("API 描述")
.build();

}

}

3、测试添加controller类

package com.liubh.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "首页模块")
@RestController
public class IndexController {

    @ApiImplicitParam(name = "name",value = "姓名",required = true)
    @ApiOperation(value = "向客人问好")
    @GetMapping("/test")
    public ResponseEntity sayHi(@RequestParam(value = "name")String name){
        return ResponseEntity.ok("Hi:"+name);
    }
}

访问:http://localhost:8084/doc.html

 常用注解:

@ApiOperationSupport(author = "xiaoymin@foxmail.com")  添加作者信息

@ApiSupport(author = "xiaoymin@foxmail.com",order = 284)

 @ApiSort(286) 排序

开启认证

官网地址:https://doc.xiaominfo.com/knife4j/documentation/get_start.html