由於前後端分離的工作模式將面向接口編程,所以接口文檔非常重要。使用swagger可是很便捷的生成接口API,並且可以通過接口界面實時查看接口,測試接口。配置也十分簡單。
1.配置增加依賴springfox-swagger2,springfox-swagger-ui
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
2.在Application.java即項目啓動入口類中使用註解@EnableSwagger2,如
package helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
配置好之後啓動項目,然後輸入地址:http://localhost/swagger-ui.html
即可訪問swagger界面,查看項目 中的所有RESTFULL接口
編碼時使用@DeleteMapping @GetMapping @PutMapping @PostMapping 代替
@RequestMapping 可以對接口進行規範分類。