栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot 整合springfox

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot 整合springfox

1 导包

    io.springfox
    springfox-boot-starter
    3.0.0

版本要选择最新的3.x.x版本

2 配置

创建一个java配置类SpringfoxConfiguration配置Docket(文档信息)

@Configuration
public class SpringfoxConfiguration {
    @Bean
    public Docket docket(){
        return new Docket(documentationType.OAS_30)
                // 文档名
                .groupName("springfox-hello")
                // 添加info信息
                .apiInfo(info())
                // 开启选择器
                .select()
                // 选择要扫描的包,包下的所有控制器都会陪扫描
                .apis(RequestHandlerSelectors.basePackage("com.aion.springfoxdemo.controller"))

                // 选择要匹配的路径
                .paths(PathSelectors.ant("/student"))
                // 匹配所有的路径
//                .paths(PathSelectors.any())
                .build();
    }

    // 创建info信息
    private ApiInfo info(){
        return new ApiInfoBuilder()
                .title("springfox test api")
                .description("这只是用来测试的简单api")
                // 添加可联系人信息
                .contact(new Contact( "aion", "http://aion.com", "aion@qq.com"))
                .version("0.1")
                .build();
    }
}
3 创建控制器
package com.aion.springfoxdemo.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Tag(name = "student", description = "学生管理")
public class StudentController {

    @Tag(name = "student")
    @Operation(summary = "一个只会返回student的api")
    @ApiResponse(responseCode = "200", description = "成功使用了此接口")
    @GetMapping("/student")
    public String get(){
        return "student";
    }
}

4 查看api文档
  • 启动项目
  • 在浏览器中输入http://localhost:8080/swagger-ui/index.htm就能查看api文档

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/287997.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号