使用swagger不用手工寫API相關的word文檔了,而且還能夠使用swagger生成的API文檔進行測試,使用起來倍兒爽。接下來我們就來搞一個demo案例。html
先是pom.xml引用和版本: java
<modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.lawt</groupId> <artifactId>swagger-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>swagger-demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> </dependencies>
springboot啓動類:linux
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SwaggerDemoApplication { public static void main(String[] args) { SpringApplication.run(SwaggerDemoApplication.class, args); } }
swagger相關掃描和配置:git
import org.springframework.beans.factory.annotation.Value; 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.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { //掃描路徑 private final String SWAGGER_SCAN_BASE_PACKAGE = "com.lawt.swaggerdemo.controller"; //開關 @Value("${swagger.enable}") private Boolean enable; @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .enable(enable) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE)) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("個人swagger demo API") .version("1.0.0") .build(); } }
一個controller類:github
import com.lawt.swaggerdemo.User; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController @Api(description = "用戶信息操做") public class UserController { @ApiOperation(value = "獲取用戶詳細信息", notes = "根據url的id來獲取用戶詳細信息") @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Integer", paramType = "path") @GetMapping("/user/{id}") public User getUserById(@PathVariable("id") Integer id) { return new User(10001, "lawt"); } }
實體類User:web
import io.swagger.annotations.ApiModelProperty; public class User { @ApiModelProperty(value = "用戶id") private Integer userId; @ApiModelProperty(value = "用戶名稱") private String userName; public User(Integer userId, String userName) { this.userId = userId; this.userName = userName; } //get set method ..... }
配置項:spring
server.port=8801 #若是線上環境不須要swagger功能,須要改爲false swagger.enable=true
啓動,並訪問:api
http://127.0.0.1:8801/swagger-ui.htmlspringboot
頁面顯示:app
點擊 Show/Hide
點擊右邊的 獲取用戶詳細信息
輸入參數
點Try it out!
顯示 linux環境下怎麼請求改接口,請求參數、返回參數等信息。
Ok,自此demo已經搞定,swagger遠不止這些功能。
能夠參考:
官網: https://github.com/swagger-api/swagger-core/wiki/Annotations-1.5.X#quick-annotation-overview
其餘參考網址:
https://blog.csdn.net/u014231523/article/details/76522486