使用Swagger生成简单接口文档

Posted liangblog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Swagger生成简单接口文档相关的知识,希望对你有一定的参考价值。

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

依赖包:

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

启动类添加配置:

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);
    }
}

配置类:

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里的接口上面添加标识

	@ApiOperation(nickname = "getList", value = "获取列表。")
	@Override
	public List<String> getList(
			@RequestBody @ApiParam(name = "请求对象", value = "传入json格式", required = true) TestRequest request) {
		return null
	}

  

 



以上是关于使用Swagger生成简单接口文档的主要内容,如果未能解决你的问题,请参考以下文章

用swagger生成接口文档代码

1 API接口文档规范Swagger3 简介

1 API接口文档规范Swagger3 简介

Swagger生成接口文档

如何使用Swagger-UI在线生成漂亮的接口文档

Spring Boot - 自动生成接口文档