swagger3+knife4j 实现接口文档

添加依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>3.0.1</version>
</dependency>

 

新建配置文件

@Configuration
@EnableOpenApi
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {

    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 创建 API 信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("标题")
                .description("描述")
                .version("1.0.0")
                .contact(new Contact("Chihiro", "http://www.ykuee.link", "chiliro@mail.com"))
                .build();
    }
}


调试

编写一个简单的RESTful接口,代码示例如下:

@Api(tags = "模块一")
@RestController
public class IndexController {

    @ApiImplicitParam(name = "name",value = "姓名",required = true)
    @ApiOperation(value = "接口描述")
    @GetMapping("/sayHi")
    public ResponseEntity<String> sayHi(@RequestParam(value = "name") String name){
        return ResponseEntity.ok("Hi:"+name);
    }
}

此时,启动Spring Boot工程,在浏览器中访问:http://localhost:8080/doc.html

版权声明:
作者:Chihiro
链接:https://www.ykuee.link/archives/399
来源:Ykuee的博客
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
海报
swagger3+knife4j 实现接口文档
添加依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> &……
<<上一篇
下一篇>>
文章目录
关闭
目 录