1 使用原因
暑期大作业中,老师希望我们使用swagger来为API写文档。
为什么要为API做文档呢?
The aggregate experience of the developer when discovering, learning to use, and finally integrating with an API is termed as Developer Experience. The API documentation is the key to excellent developer experience.
2 Swagger配置
参照了这个链接的内容进行使用:swagger与SpringBoot项目的整合
首先引入 maven dependency
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
接着在SpringBoot的启动class中enable Swagger
@EnableSwagger2
@SpringBootApplication
public class WeiboserviceApplication {
public static void main(String[] args) {
SpringApplication.run(WeiboserviceApplication.class, args);
}
}
更好的方法是 build a seperate config class
在这里,我们指定了API暴露(exposed)的地址,设置了包名称
import org.springframework.context.annotation.Bean;
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.ApiInfo;
import springfox.documentation.service.Contact