使用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 }