首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >使用Swagger生成简单接口文档

使用Swagger生成简单接口文档

作者头像
JQ实验室
发布2022-02-09 17:43:58
发布2022-02-09 17:43:58
9780
举报
文章被收录于专栏:实用技术实用技术

使用swagger通过简单的配置可以生成简单的接口文档;

依赖包:

// Swagger2 compile 'io.springfox:springfox-swagger2:2.8.0' compile 'io.springfox:springfox-swagger-ui:2.8.0'

启动类添加配置:

代码语言:javascript
复制
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableAutoConfiguration
public class ShowCaseApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(ShowCaseApplication.class, args);
    }
}

配置类:

代码语言:javascript
复制
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;


import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@ComponentScan(basePackages = { //"com.a.controller", //
        })
@EnableSwagger2
public class AutoConfiguration {

    @Bean
    public Docket docketCommon() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("test").select()
                .apis(RequestHandlerSelectors.basePackage("com.a.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(new ApiInfoBuilder().
                        title("test Restful API").
                        description("接口API").
                        contact(new Contact("", "", "")).
                        version("1.0").
                        build());
    }
}

接口注释:在controller里的接口上面添加标识

代码语言:javascript
复制
	@ApiOperation(nickname = "getList", value = "获取列表。")
	@Override
	public List<String> getList(
			@RequestBody @ApiParam(name = "请求对象", value = "传入json格式", required = true) TestRequest request) {
		return null
	}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-09-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档