POM
增加 Swagger2 所需依赖,pom.xml
配置如下:
<!-- Swagger2 Begin -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- Swagger2 End -->
配置 Swagger2
注意:RequestHandlerSelectors.basePackage("com.yuu.ymall.controller")
为 Controller 包路径,不然生成的文档扫描不到接口
创建一个名为 Swagger2Config
的 Java 配置类,代码如下:
package com.yuu.ymall.common.swagger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc