1.添加依赖
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> </dependency>
2.配置类
package com.cj.system.config; 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.Contact; 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; @Configuration @EnableSwagger2WebMvc public class Knife4jConfig { @Bean public Docket adminApiConfig(){ List<Parameter> pars = new ArrayList<>(); ParameterBuilder tokenPar = new ParameterBuilder(); tokenPar.name("token") .description("用户token") .defaultValue("") .modelRef(new ModelRef("string")) .parameterType("header") .required(false) .build(); pars.add(tokenPar.build()); //添加head参数end Docket adminApi = new Docket(DocumentationType.SWAGGER_2) .groupName("adminApi") .apiInfo(adminApiInfo()) .select() //只显示admin路径下的页面 .apis(RequestHandlerSelectors.basePackage("com.cj")) .paths(PathSelectors.regex("/admin/.*")) .build() .globalOperationParameters(pars); return adminApi; } private ApiInfo adminApiInfo(){ return new ApiInfoBuilder() .title("后台管理系统-API文档") .description("本文档描述了后台管理系统微服务接口定义") .version("1.0") .contact(new Contact("cj", "http://cj.com", "cj@qq.com")) .build(); } }
3.加注解
package com.cj.system.controller; import com.atguigu.model.system.SysRole; import com.cj.system.mapper.SysRoleMapper; import com.cj.system.service.SysRoleService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @Api(tags = "角色管理") @RestController @RequestMapping("admin/system/sysRole") public class SysRoleController { @Autowired private SysRoleService sysRoleService; @ApiOperation(value = "获取全部角色列表") @GetMapping("findAll") public List<SysRole> findAll(){ List<SysRole> list = sysRoleService.list(); return list; } @DeleteMapping("remove/{id}") public boolean remove(@PathVariable("id") Long id){ boolean b = sysRoleService.removeById(id); return b; } }
4.访问
localhost:8800/doc.html
标签:cj,knife4j,springboot,documentation,整合,new,import,springfox,com From: https://www.cnblogs.com/cciscc/p/16834986.html