javascript
Spring Boot + Swagger
http://springfox.github.io/springfox/docs/current/#customizing-the-swagger-endpoints
一、什么是Swagger?
Swagger:? 一個描述Restful service規范
?????????????? 可以在界面上測試RestfulService
?
二、Swagger 和 Springfox-Swagger
What is the relationship between swagger-ui and springfox-swagger-ui?
??? Swagger Spec is a specification.
??? Swagger Api - an implementation of that specification that supports jax-rs, restlet, jersey etc.
??? Springfox libraries in general - another implementation of the specification focused on the spring based ecosystem.
??? Swagger.js and Swagger-ui - are client libraries in javascript that can consume swagger specification.
??? springfox-swagger-ui - the one that you’re referring to, is just packaging swagger-ui in a convenient way so that spring services can serve it up.
翻譯下來就是:
swagger-ui 和 springfox-swagger-ui 的關系是?
??? Swagger Spec 是一個規范。
??? Swagger Api 是 Swagger Spec 規范 的一個實現,它支持 jax-rs, restlet, jersey 等等。
??? Springfox libraries 是 Swagger Spec 規范 的另一個實現,專注于 spring 生態系統。
??? Swagger.js and Swagger-ui 是 javascript 的客戶端庫,能消費該規范。
??? springfox-swagger-ui 僅僅是以一種方便的方式封裝了 swagger-ui ,就是可以顯示文檔內容
?
三、Springboot 與Swagger集成
1. 增加下面maven 引用
<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>?
2. 增加一個Swaagger configuration
?
package com.example.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;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;@Configuration public class Swagger2UiConfiguration extends WebMvcConfigurerAdapter {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("org.springframework.boot")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("springboot利用swagger構建api文檔").description("簡單優雅的restful風格,http://XX").termsOfServiceUrl("http://XXx").version("1.0").build();}}?3.Spring Boots Application 類增加 @EnableSwagger2
?
?
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;import springfox.documentation.swagger2.annotations.EnableSwagger2;@SpringBootApplication @EnableSwagger2 public class UserApplication1 {public static void main(String[] args) {SpringApplication.run(UserApplication1.class, args);} }?4. 在Controller 中增加相對應的API注釋
package com.example.demo;import java.util.concurrent.atomic.AtomicLong;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation;@RestController public class GreetingController {private static final String template = "Hello, %s!";private final AtomicLong counter = new AtomicLong();@ApiOperation(value="獲取Greeting info", notes="根據name獲取greeting info")@ApiImplicitParam(name = "name", value = "用戶名", required = true, dataType = "String", paramType = "path")@RequestMapping("/greeting")public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {return new Greeting(counter.incrementAndGet(), String.format(template, name));} }?部署啟動服務器
訪問: http://localhost:8080/swagger-ui.html#
?
四、Swagger注解
?
swagger通過注解表明該接口會生成文檔,包括接口名、請求方法、參數、返回信息的等等。
?
- @Api:修飾整個類,描述Controller的作用
- @ApiOperation:描述一個類的一個方法,或者說一個接口
- @ApiParam:單個參數描述
- @ApiModel:用對象來接收參數
- @ApiProperty:用對象接收參數時,描述對象的一個字段
- @ApiResponse:HTTP響應其中1個描述
- @ApiResponses:HTTP響應整體描述
- @ApiIgnore:使用該注解忽略這個API
- @ApiError :發生錯誤返回的信息
- @ApiImplicitParam:一個請求參數
- @ApiImplicitParams:多個請求參數
總結
以上是生活随笔為你收集整理的Spring Boot + Swagger的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot CMI 使用笔记
- 下一篇: 分步式事务