引入jar
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
写配置文件
@Configuration
@EnableSwagger2
public class Swagger {
@Bean
public Docket buildDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())//调用下面apiInfo()方法
.select()
//Controller所在路径
.apis(RequestHandlerSelectors.basePackage("com.young.web"))
.paths(PathSelectors.any())
.build();
}
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("srpingmvc结合swagger2构建Restful API")
// http://localhost:8080/swagger-ui.html
.termsOfServiceUrl("http://localhost")
.version("0.0.1")
.description("API接口")
.build();
}
}
spring*.xml配置
<!--启动swagger2 -->
<bean class="com.young.util.Swagger"/>
编写接口文档
@GetMapping("/getshoplist")
@ApiOperation("获取店铺列表")
private Map<String, Object> getShopList(HttpServletRequest request) {
……
}
访问
端口后面有系统名则需要加上
http://localhost:8080/swagger-ui.html