码迷,mamicode.com
首页 > 其他好文 > 详细

使用Swagger生成简单接口文档

时间:2018-09-27 22:53:18      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:添加   json格式   des   highlight   figure   sharp   ram   script   rgs   

使用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生成简单接口文档

标签:添加   json格式   des   highlight   figure   sharp   ram   script   rgs   

原文地址:https://www.cnblogs.com/liangblog/p/9716064.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!