ZhuangRenyang / blog

蝉时雨网站文章仓库
https://ovoz.cn
3 stars 0 forks source link

Knife在线API文档 #21

Open ZhuangRenyang opened 1 year ago

ZhuangRenyang commented 1 year ago

XML配置

<!-- Knife Spring Boot 在线API文档-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.9</version>
        </dependency>

Controller控制器Api注解配置

类配置 :Api@Api(tags = "xxx")

方法配置 : @ApiOperation("xxx") , @ApiOperationSupport(order = 1~n)

@Slf4j
//@Lazy
@Api(tags = "3.图片管理模块")
@RestController
@RequestMapping("/album")
public class AlbumController {
    public AlbumController() {
        log.info("创建相册控制器对象.AlbumController");
    }
    @Autowired
    private IAlbumService albumService;

    public AlbumController(IAlbumService albumService) {
        log.info("创建相册控制器对象.AlbumController");
    }
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 10)
    @PostMapping("/add-new")
    public String addNew(AlbumAddNewDTO albumAddNewDTO){
         albumService.addNew(albumAddNewDTO);
    }
}

Knife4jConfiguration配置类

@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {
//测试 http://localhost:9080/doc.html#/home
    /**
     * 【重要】指定Controller包路径
     */
    private String basePackage = "cn.tedu.jsdvn2203.csmall.server.controller";//导入控制器包
    /**
     * 分组名称
     */
    private String groupName = "product";
    /**
     * 主机名
     */
    private String host = "http://java.tedu.cn";
    /**
     * 标题
     */
    private String title = "酷鲨商城在线API文档--商品管理";
    /**
     * 简介
     */
    private String description = "酷鲨商城在线API文档--商品管理";
    /**
     * 服务条款URL
     */
    private String termsOfServiceUrl = "http://www.apache.org/licenses/LICENSE-2.0";
    /**
     * 联系人
     */
    private String contactName = "Java教学研发部";
    /**
     * 联系网址
     */
    private String contactUrl = "http://java.tedu.cn";
    /**
     * 联系邮箱
     */
    private String contactEmail = "java@tedu.cn";
    /**
     * 版本号
     */
    private String version = "1.0.0";

    @Autowired
    private OpenApiExtensionResolver openApiExtensionResolver;

    @Bean
    public Docket docket() {
        String groupName = "1.0.0";
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .host(host)
                .apiInfo(apiInfo())
                .groupName(groupName)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build()
                .extensions(openApiExtensionResolver.buildExtensions(groupName));
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .termsOfServiceUrl(termsOfServiceUrl)
                .contact(new Contact(contactName, contactUrl, contactEmail))
                .version(version)
                .build();
    }

}