SpringBoot中整合knife4j接口文档和调试

1.导入maven包

        <!-- 接口文档生成 -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>
        <!-- 避免版本冲突 -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>29.0-jre</version>
        </dependency>

2.knife4j 配置文件

/**
 * knife4j 配置
 *
 * @Author wb
 */
@Configuration
@EnableSwagger2
public class Knife4jConfig {
     

    @Bean
    public Docket createRestApi() {
     
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.XXX"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
     
        return new ApiInfoBuilder()
                .title("SpringBoot项目 后台服务API接口文档")
                .description("使用 knife4j 搭建的后台服务API接口文档")
                .termsOfServiceUrl("http://localhost:8080/")
                .contact("wb")
                .version("1.0.0")
                .build();
    }
}

3.启动类

/**
 * @author wb
 * @Date 2020-08-11 15:52:21
 */
@SpringBootApplication
@EnableSwagger2
@EnableCaching
@EnableAsync
@ConditionalOnClass(SpringfoxWebMvcConfiguration.class)
public class LockApiApplication implements WebMvcConfigurer {
     

    public static void main(String[] args) {
     
        SpringApplication.run(LockApiApplication.class, args);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
     
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}

4.放开拦截器

/**
 * @author wb
 * @Date 2020-08-11 15:52:21
 */
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
     

    @Bean
    public WebMvcConfigurer getInterfaceAuthCheckInterceptor() {
     
        return new WebMvcConfigurer();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
     
        /** 指定拦截器类 */
        registry.addInterceptor(new BootInterceptor())
                /** 指定该类拦截的url */
                .addPathPatterns("/**")
                .excludePathPatterns("/doc.html")
                .excludePathPatterns("/service-worker.js")
                .excludePathPatterns("/swagger-resources")
                .excludePathPatterns("/webjars/**");
        ;
    }
}
/**
 * @author wb
 * @Date 2020-08-11 15:52:21
 */
@Configuration
@Slf4j
public class MyShiroFilter {
     
	@Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
     
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

        Map<String, Filter> map = new LinkedHashMap<String, Filter>();
        map.put("authc", getFormAuthenticationFilter());
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap();
        filterChainDefinitionMap.put("/doc.html", "anon");
        filterChainDefinitionMap.put("/service-worker.js", "anon");
        filterChainDefinitionMap.put("/swagger-resources", "anon");
        //配置在最后面
        filterChainDefinitionMap.put("/**", "authc");

        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //这个map中包含了上面自定义的信息,配置到setFilter中
        shiroFilterFactoryBean.setFilters(map);
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        return shiroFilterFactoryBean;
    }
}

4.使用

@Api,整个类的注释
@ApiOperation,方法上的注释
@ApiImplicitParams,参数列表的注释
@ApiImplicitParam,每一个参数的注释

/**
 * 

* 用户信息表 前端控制器 *

* * @author wb * @since 2020-08-11 */
@Api(tags = "后台管理端--用户模块") @Controller @RequestMapping("/mgt-user") public class MgtUserController { @ApiOperation(value = "分页查询用户数据", notes = "分页查询用户数据") @ApiImplicitParams({ @ApiImplicitParam(name = "page", value = "页码数", required = true), @ApiImplicitParam(name = "limit", value = "每页条数", required = true) }) @GetMapping("list") @ResponseBody public ResponseVo listUser(int page, int limit) { return new ResponseVo().ok("qwe"); } }

实体

@ApiModel,实体类的注解
@ApiModelProperty,字段的注解

/**
 * 系统角色Search
 * @author wb
 * @Date 2020-08-11 15:52:21
 */
@Data
@ApiModel(value = "SysRoleSearch")
public class SysRoleSearch extends BasePageDto {
     

    private static final long serialVersionUID = 1L;

    /** 角色名称 */
    @ApiModelProperty(value = "角色名称")
    private String name = "";
   
    /** 排序 */
    @ApiModelProperty(value = "排序")
    private int sort;

    public SysRole toSysRole() {
     
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(this, sysRole);
        return sysRole;
    }
}

成功了 - -

http://localhost:8089/doc.html
SpringBoot中整合knife4j接口文档和调试_第1张图片
SpringBoot中整合knife4j接口文档和调试_第2张图片

你可能感兴趣的:(java)