引入pom文件依赖:
<!-- swagger -->
<!-- 文档自动生成 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
配置文件:
@Configuration
@EnableSwagger2
@ComponentScan("com.hexun.bdc.auth")
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.hexun.bdc.auth.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("CashBack's RESTful APIs")
.description("更多请关注:http://localhsot/")
.termsOfServiceUrl("http://bdcresttest.hexun.com/cashback2server")
.version("1.0")
.build();
}
}
在浏览器中输入即可访问了:
http://localhost:8010/swagger-ui.html
swagger使用:
1、 接口注释@ApiOperation(value="获取用户列表", notes="")
image.png
2、类注解:@Api
用在类上,说明该类的作用
比如说:@Api(value = “UserController”, description = “用户相关api”)
@Api(value = "MaterialController ",description = "用户相关接口")
@Controller
@RequestMapping("user")
public class UserController {
}
网友评论