关于springboot:Spring-Boot结合Swegger

增加依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>

配置SweggerConfig


/**
 * @Api: 用于类,标识这个类是swagger的资源
 * @ApiIgnore: 用于类,疏忽该 Controller,指不对以后类做扫描
 * @ApiOperation: 用于办法,形容 Controller类中的 method接口
 * @ApiParam: 用于参数,单个参数形容,与 @ApiImplicitParam不同的是,他是写在参数左侧的。如( @ApiParam(name="username",value="用户名")Stringusername)
 * @ApiModel: 用于类,示意对类进行阐明,用于参数用实体类接管
 * @ApiProperty:用于办法,字段,示意对model属性的阐明或者数据操作更改
 * @ApiImplicitParam: 用于办法,示意独自的申请参数
 * @ApiImplicitParams: 用于办法,蕴含多个 @ApiImplicitParam
 * @ApiResponse: 用于办法,形容单个出参信息
 * @ApiResponses: 用于办法,蕴含多个@ApiResponse
 * @ApiError: 用于办法,接口谬误所返回的信息
 *
 *
 * Failed to start bean ‘documentationPluginsBootstrapper‘;解决办法
 * 在application.properties里配置里增加:
 * spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
 * 起因: 这是因为Springfox应用的门路匹配是基于AntPathMatcher的,而Spring Boot 2.6.X应用的是PathPatternMatcher。
 */

@Configuration // 表明是配置类
@EnableSwagger2 //开启swagger性能
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)  // DocumentationType.SWAGGER_2 固定的,代表swagger2
//                .groupName("分布式工作零碎") // 如果配置多个文档的时候,那么须要配置groupName来分组标识
                .apiInfo(apiInfo()) // 用于生成API信息
                .select() // select()函数返回一个ApiSelectorBuilder实例,用来管制接口被swagger做成文档
                .apis(RequestHandlerSelectors.basePackage("com.jsd.controller")) // 用于指定扫描哪个包下的接口
                .paths(PathSelectors.any())// 抉择所有的API,如果你想只为局部API生成文档,能够配置这里
                .build();
    }

    /**
     * 用于定义API主界面的信息,比方能够申明所有的API的总题目、形容、版本
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("测试") //  能够用来自定义API的主题目
                .description("XX我的项目SwaggerAPI治理") // 能够用来形容整体的API
                .termsOfServiceUrl("") // 用于定义服务的域名
                .version("1.0") // 能够用来定义版本。
                .build(); //
    }
}

Testcontroller

@Api(tags = "测试治理")
@RestController
public class TestController {

    @ApiOperation("测试方法")
    @GetMapping("/test")
    public String Test01(){
        return "Test01";
    }

}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理