spring boot 中Swagger的使用

在开发过程当中方便测试
一、依赖的包:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spring-web</artifactId>
<version>2.6.1</version>
</dependency>

二、配置类:
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket customDocket() { return new Docket(DocumentationType.SWAGGER_2).groupName("situation") .genericModelSubstitutes(DeferredResult.class).useDefaultResponseMessages(false).forCodeGeneration(true) .select().apis(RequestHandlerSelectors.basePackage("com.huawei.situation.controller"))// 选择哪些路径和API会生成document
                .paths(PathSelectors.any())// 对全部路径进行监控
 .build().apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Situation API") .description("TCB_Site_Situation Rest API with Swagger") .termsOfServiceUrl("/Situation/") .contact(new Contact("lwx480324", "", "")) .license("TCB_Site_Situation License") .licenseUrl("") .version("1.0") .build(); } }三、在controller类上使用@Api定义controller的基本信息,在相关api接口上使用@ApiOperation、@ApiResponses来设置方法的一些基本信息、响应类型等信息四、localhost:port/swagger-ui.html访问页面!
相关文章
相关标签/搜索